strange behevior of the JVM or compiler...

T

Thomas

Hello again :). I m writing code like this using JCreator :


class dummy{
public final static long mod = (long)2 * 3 * 5;

}
and then from the second class I call :

System.out.println(dummy.mod);
and if I do try to change the value of ' mod ' in code by changing
initialization line I STILL GET THE OLD VALUE NO MATTER HOW MANY TIMES I DO
COMPILE AND RUN. Is it the bug in JVM or in my IDE ?
 
S

Stefan Ram

Thomas said:
i still get the old value no matter how many times i do

The value is literally inserted into the site where it is
used.

Either force recompilation, by deleting /all/ class files
or invoke a get method of the class providing the value
instead of a direct access to the value name.
 
K

~kurt

Thomas said:
System.out.println(dummy.mod);
and if I do try to change the value of ' mod ' in code by changing
initialization line I STILL GET THE OLD VALUE NO MATTER HOW MANY TIMES I DO
COMPILE AND RUN. Is it the bug in JVM or in my IDE ?

Try compiling dummy.java from the command line with javac. You could be
using the IDE wrong, or the IDE might have issues.

Also, it is more Java'ish to name the class Dummy, not dummy.

- Kurt
 
R

Roedy Green

class dummy{
public final static long mod = (long)2 * 3 * 5;

}
and then from the second class I call :

System.out.println(dummy.mod);
and if I do try to change the value of ' mod ' in code by changing
initialization line I STILL GET THE OLD VALUE NO MATTER HOW MANY TIMES I DO
COMPILE AND RUN. Is it the bug in JVM or in my IDE ?
cm
I see two problems. First dummy should be Dummy. See
http://mindprod.com/jgloss/naming.html


Second, static finals get turned into literals (constants) even when
they are referred outside the class. If you modify them, you must
recompile the universe to make sure the new value gets propagated.

If you look inside the class file for your second class, you will see
code like this println(30) and you will see it has not been recompiled
since you changed the value of mod.

I discuss this is greater depth in
http://mindprod.com/jgloss/javacexe.html

The rule of thumb is, when every you change public statics, recompile
everything, deleting all class files before you start.

The other rule of thumb is, when you see anomalies, first try a global
clean compile before you waste too much time trying to chase it down.
 
T

Twisted

Second, static finals get turned into literals (constants) even when
they are referred outside the class. If you modify them, you must
recompile the universe to make sure the new value gets propagated.

Efficient though this is, it seems to mean that for "constants" that
you expect might change in a future version of a library you should
provide accessor methods and not a public static final field.
Otherwise, drop-in replacement of the older version with the newer
version in a deployed app's classpath might fail in bizarre ways. To
make sure newer versions of a library are binary-compatible with apps
that use the library, and not just source-compatible, requires not
changing the values of public static final fields.

So you'd want to use

public static final int MAX_FACTORIAL = 12; // 12! is less than
// Integer.MAX_VALUE; 13! is greater; and this will never change.

public static int getMaxTreeDepth () {
return 5;
// In later versions this might be increased to 6 or even 7.
}
 
M

Manivannan Palanichamy

Thomas said:
Hello again :). I m writing code like this using JCreator :


class dummy{
public final static long mod = (long)2 * 3 * 5;

}
and then from the second class I call :

System.out.println(dummy.mod);
and if I do try to change the value of ' mod ' in code by changing
initialization line I STILL GET THE OLD VALUE NO MATTER HOW MANY TIMES I DO
COMPILE AND RUN. Is it the bug in JVM or in my IDE ?


Thats not a compiler Bug. Thats a compiler optimization trick. It is
called code 'inlining'. Means, as you declared the 'mod' as final, it
is considered as a constant. So, the compiler calculates the value of
mod, and replaces in place of all of its usage at compile time, (not
runtime). Note, It is not substituted in runtime, only at compile
time.

I can explain you in this way. Suppose Class A has a final qualified
constant member 'a' and Class B uses the constant A.a,

1. When you compile Both Class A & B, the compiler replaces all
occurance of 'A.a' (in class B )by its value, at compile time.
2. If you modify the class A.a 's value and just compile it, that
change will not reflect in Class B. Because, the change is made at
compile time only -- means when you recompile B, the change will
reflect.
3. You have to just edit the Class B and save it, then recompile class
B. Now you will see, the change made to A.a, taking effect in Class B.
(If you just recompile B, some times ant builds don't recompile it.
So, edit B, save, and then recompile)
 
L

Lew

Manivannan said:
(If you just recompile B, some times ant builds don't recompile it.
So, edit B, save, and then recompile)

No need to edit B like that, just delete B.class (and A.class) then recompile
both A.java and B.java, as others have suggested on this topic.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top