Re: strange behevior of the JVM or compiler...




Thomas wrote:
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)


--
Manivannan.Palanichamy (@) Oracle.com
http://mani.gw.googlepages.com/index.html

.



Relevant Pages

  • Re: creaping coupling......
    ... In C++ it would be trivial to break that source code dependency, ... When you compile a .java module, ... In Java and C# it's asserted at compile time. ...
    (comp.object)
  • Re: VB Compiler Warnings & other woes ...
    ... you will get this warning in vs 2005. ... > have a direct legacy language path of evolution to consider. ... >> compile time errors rather than runtime. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: No exceptions?
    ... time that was removed before runtime; there's no way to generate a compile time exception for the unexceptionable, but there are not many ways of avoiding a runtime exception if the compile time y had defined attributes. ... Thus, as soon as one compiles a statement that references any such resource, one would get a compile-time error. ... I think many products go to elaborate lengths and introduce arbitrary concepts to handle such cases, at least some of them try to prevent catalogue updates in a transaction (Jim Gray described a logical '2-phase' protocol for intension locks many years ago and I've seen it implemented and I've seen the complications that seem never to end due to the intricacy, for example a reversal of Jonathan's converse problem, a sequential transaction that references y and then deletes y's definition - what should happen if the reference is then referred to again? ...
    (comp.databases.theory)
  • Re: apparently undefined function called by macro
    ... However, as almost always in Common Lisp;), there are exceptions to this rules: Some top-level forms do indeed have effects at compile time. ... The reason is that you typically want to base subsequent code on your macro definitions, and the compiler must be able to completely macro-expand away these macro definitions as well. ... A system definition allows you to declare that one file of lisp code depends on some other file of lisp code. ...
    (comp.lang.lisp)
  • Re: RAD vs. performance
    ... iterable object--whether or not the object is an array or list is ... return ret ... known at compile time. ...
    (comp.lang.misc)