aspose file tools*
The moose likes Java in General and the fly likes Static Import in JDK 5 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Static Import in JDK 5" Watch "Static Import in JDK 5" New topic
Author

Static Import in JDK 5

Jobin Babu
Greenhorn

Joined: Oct 20, 2006
Posts: 7
Hi All,

I am facing a strange scenario in JDK 1.5

I have a constant class with variable


I imported the STATUS variable in another class using static import (Let’s say the Class Name as A). I compiled both classes and executed. Later I changed the STATUS value into "SUCCESS". I re-compiled the Constant class and executed again. But the change is not reflecting in the class A. Later I recompiled class A and it’s started working. Any idea what’s the necessary to compile the class A??

Any suggestions on this??

Thanks in Advance
Jobin


SCJP 5 - SCWCD 5
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

For compile time constants, which include Strings, primitives, and any String / primitive created by applying operators on them (e.g. + for String concatenation, mathematical operators), the compiler has a tendency to replace calls to those constants to their actual values.
So the compiled class A did not make a call to Constant.STATUS, but actually includes the string value "ERROR".

If you know the constants are going to change, you can use a static initializer block:

Because the initialization of STATUS is separated from the declaration, the compiler no longer does the substitution but will look for the latest value each time.

Note that you need to initialize the "constant" in a static block, otherwise the code won't compile because STATUS may not be initialized.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
It is worthwhile doing a javap call on the .class file. Try javap -c MyClass and you can see whether the compile-time constants have been "inlined." As Rob said, the compiler "doesn't expect the values to change."
Jobin Babu
Greenhorn

Joined: Oct 20, 2006
Posts: 7
This is happening in one of my Real time application. I am using this contant in more than a dozen of classes. Its not a good idea to go and re-compile all the classes.

I beleive it is indirectly preventing the concept of "Reusability" ???
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

It's documented and well-known behavior, and Rob describes a simple and well-known approach to "fixing" it if you must.


[Jess in Action][AskingGoodQuestions]
Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
Final objects can't have it value changed.

If you have a lot of static imports and final Strings like that maybe it should be time for some refactoring..

You never thinked about have an Singleton who stores variables like that to use in all the program?
It's a solution.. and some others.


Each of their nuggets of wisdom contracted to a sound bite: Joshua Bloch: Write Lots of Code; Chet Haase: Don't Put Your Entire Application in One Method; Masood Mortazavi: Start Simple and Keep Learning; Cay Horstmann: First, Don't Panic
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
Ah can you explain this Rob?
If you know the constants are going to change, you can use a static initializer block:


I dont get it....how can the value of the STATUS variable change? Its marked final ?

Thanks


<a href="http://faq.javaranch.com/java/UseCodeTags" target="_blank" rel="nofollow">Use Code Tags!!</a>
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Tom Johnson wrote:Ah can you explain this Rob?


I dont get it....how can the value of the STATUS variable change? Its marked final ?



Read the original post in this thread. We're talking about what happens when the code is edited and a new value is used, then this class is recompiled, but other classes aren't.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

It isn't changed, it's just set in a different way.

The static initializer block is called only once, when the class is initialized. Because it is only called once STATUS only has its value set once, just like with the declare+initialize combination. The class initialization is guaranteed to complete, including all static initializer blocks, before any of its methods is called or any of its fields is evaluated, so by the time some outside class calls Constant.STATUS, this field has its only value set.
But because the declaration and initialization are split, the compiler does not know its value, and therefore cannot replace it in calling code. This is why in this example STATUS is not a so-called compile time constant but a runtime constant. If you need more info on the difference, use our search - there was a real big discussion on it last year.
Tom Johnson
Ranch Hand

Joined: May 11, 2005
Posts: 142
Ah right OK thanks guys
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static Import in JDK 5