behavior of public static final int
I think this is something I read about, but I can't find any references.
I've made two files, test.java, and const.java.
Here's test.java:
--------------------------------------------------------------------
public class test {
public static void main(String[] args) {
System.out.println( "Const T = " + Const.T + "\n");
}
}
--------------------------------------------------------------------
and here's const.java
--------------------------------------------------------------------
class Const {
public static final int T = 40;
}
--------------------------------------------------------------------
Now, I compile both of them with javac *.java.
I have two class files, test.class, and const.class.
Now, suppose I change the value of T in const.java to 10, and I
recompile just const.java with javac const.java.
If I now disassemble test.java, with javap -s -l -c -verbose test, I get
--------------------------------------------------------------------
Compiled from "test.java"
public class test extends java.lang.Object
SourceFile: "test.java"
minor version: 0
major version: 50
Constant pool:
const #1 = Method #6.#15; // java/lang/Object."<init>":()V
const #2 = Field #16.#17; //
java/lang/System.out:Ljava/io/PrintStream;
const #3 = String #18; // Const T = 40\n
const #4 = Method #19.#20; //
java/io/PrintStream.println:(Ljava/lang/String;)V
const #5 = class #21; // test
const #6 = class #22; // java/lang/Object
const #7 = Asciz <init>;
const #8 = Asciz ()V;
const #9 = Asciz Code;
const #10 = Asciz LineNumberTable;
const #11 = Asciz main;
const #12 = Asciz ([Ljava/lang/String;)V;
const #13 = Asciz SourceFile;
const #14 = Asciz test.java;
const #15 = NameAndType #7:#8;// "<init>":()V
const #16 = class #23; // java/lang/System
const #17 = NameAndType #24:#25;// out:Ljava/io/PrintStream;
const #18 = Asciz Const T = 40\n;
const #19 = class #26; // java/io/PrintStream
const #20 = NameAndType #27:#28;// println:(Ljava/lang/String;)V
const #21 = Asciz test;
const #22 = Asciz java/lang/Object;
const #23 = Asciz java/lang/System;
const #24 = Asciz out;
const #25 = Asciz Ljava/io/PrintStream;;
const #26 = Asciz java/io/PrintStream;
const #27 = Asciz println;
const #28 = Asciz (Ljava/lang/String;)V;
{
public test();
Signature: ()V
LineNumberTable:
line 1: 0
Code:
Stack=1, Locals=1, Args_size=1
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 1: 0
public static void main(java.lang.String[]);
Signature: ([Ljava/lang/String;)V
LineNumberTable:
line 5: 0
line 6: 8
Code:
Stack=2, Locals=1, Args_size=1
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3; //String Const T = 40\n
5: invokevirtual #4; //Method
java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
LineNumberTable:
line 5: 0
line 6: 8
}
--------------------------------------------------------------------
As you can see above, even though I've changed the value of T to 10,
inside of const.java, and recompiled it to get a new const.class, it is
still set to 40 in test.class.
So it seems that literal values are stored in the class file at compile
time..
I'm not crazy about this, and I never gave it much of a thought before.
Actually, I think that I read about this somewhere, somewhen, but I
can't recall where.
I guess I'll be certain to do 'ant clean' and 'ant jar' from now on.
Is there some work around for this, other than just recompiling
everything everytime?
I would prefer that the constants would be referenced out of their own
..class file everytime, rather than compiled into the code, although I
guess that _is_ what a compiler is for...
TIA,
Dave in Largo, FL