Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

compiling packages

 
Peter Straw
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Straw
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Straw
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic