0

My question is very simple ! I have seen several applications that define database name(or table name) as a static variable ! why ?

like :

private static final String DATABASE_NAME   = "database" ;

can I define it as a final variable ?

S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254

2 Answers2

2

Possibly this way you want to say that your DATABASE_NAME is not part of every instance of your class, but a part of the class itself. That would mean that each time you declare an instance of your class, each of those classes won't have a copy of that attribute but they will share that attribute instead.

Quite logic, this is meant to be a static field so why make an instance of it? And yes, it should be a final variable, as it is unlike it will change at execution time.

nKn
  • 13,691
  • 9
  • 45
  • 62
2

For convenience. The DB name never changes while the app is running or between instances of the same class - it can only change between app versions. This way if you change the name of your database you won't have to search for it in the entire app - just modify the DATABASE_NAME variable.