This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I have some final static variables in class A in package A, that are imported and referenced by class B in package B. If I change the values of these variables in class A and then recompile class A, do I also have to recompile class B or will it reference the new values of these variables at runtime? Many thanks [ January 07, 2003: Message edited by: Peter Straw ] [ January 07, 2003: Message edited by: Peter Straw ]
Yes, you must recompile B. For optimization purposes, static final field values are inserted into the class bytecode when the class is compiled. I like to think of them as being "hard-coded" into the bytecode during compilation, so you have to recompile in order to "hard-code" a different value.
Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
This is an issue with Binary Compatibility. Technically NO you do not have to recompile the B classes unless, of course, that you really WANT the to pick up the new value . From the Java Language Specification 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.
"JavaRanch, where the deer and the Certified play" - David O'Meara
Joined: Jan 08, 2002
Thanks a lot Blake and Cindy. I DO want the new values to be picked up, so is there a way of storing and referencing these variables without having to re-compile the other package(s). Perhaps they should just be private member variables with get/set methods?
Joined: Sep 13, 2002
Originally posted by Peter Straw: Perhaps they should just be private member variables with get/set methods?
Hmmm... do those values change often? If so then that's a different story, I'd recommend parameterizing them somehow (e.g. props file) and making them private members with get/set methods as you said.
Joined: Jan 08, 2002
No, the values don't change once the application has been installed. I'm not familiar with a Props file, perhaps that is the best solution though. I'll look into this, thanks.
Joined: Sep 29, 2000
I think that he means that you can use a Properties file where the value of the property is read from a file (usually at start up).