[Logo]
Forums Register Login
Constaints during compile time
==========
Step 1
==========
File A.java public static final int X = 100;

File B.java - makes reference to A.X


These two files are stored in App1.jar


==========
Step 2
==========
The above 2 files (File A.java & B.java are copied to a new location)

File A.java is modifed to change X
public static final int X = 200; (200 instead of 100)
It also includes println commands.
System.out.println("Inside modified module");

File B.java - makes reference to A.X

Note: during compile time the CLASSPATH is still set to reference the original A.java.

These two files are stored in App2.jar


==========
Step 3
==========
Run the application using App2.jar

java -classpath /usr/bin/App2.jar .....

I know the "modified" file is being referenced because I get output "Inside modified module" but when referencing the constant variable it still is 100 instead of 200.


============
Question
============
Although I can fix it by compiling with the CLASSPATH pointing to the modified file, my question is should it matter?

During compile time shouldn't it just verify that the variable exists or does it also store all constants BEFORE the application is even executed?
Constants, such as the public static final int X in your example, are inlined at compile time.

That means that if you compile class A and class B, then you edit class A and you only re-compile class A (not class B), the old value will still be in class B. You'll need to recompile class B too to make it see the new value.
Thanks for clarifying as that makes perfect sense.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


This thread has been viewed 505 times.

All times above are in ranch (not your local) time.
The current ranch time is
Sep 20, 2018 02:29:17.