Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

static keys  RSS feed

 
Monde Hans
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just seen one of our developers having a class with all his keys as public static final Strings. I do not have a problem with that. What I want to know is wheather he will not need to recompile all his files when he changes the values in the keys class. I had this problem with my debug code the other day.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65825
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm understanding what you are saying, he has defined all keys (such as request parameter keys, attribute keys, and so on) as exported constants.
This is a good practice. While it is true that a recompile will be necessary if the values of thsoe keys are changed, that's not something that often happens. In fact, if the key strings are wisely chosen in the first place they never need to be changed.
By using exported constants, one avoids the tedious series of inevitable bugs that occur when the keys are accidentally spelled (or cased) in different ways in the various places where they are referenced.
hth,
bear
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do not recompile the other classes will work just fine, they will just continue to use the old values.
This is discussed in the JLS under "Binary Compatibility" 13.4.8 final Fields and Constants.
If a field is a compile-time constant, then deleting the keyword final or changing its value will not break compatibility with pre-existing binaries by causing them not to run, but they will not see any new value for the constant unless they are recompiled.

However the JLS also says not to use them this way much:
Other than for true mathematical constants, we recommend that source code make very sparing use of class variables that are declared static and final. If the read-only nature of final is required, a better choice is to declare a private static variable and a suitable accessor method to get its value.

Although, as Bear suggests, in real life static final fields are used much more than this indicates .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!