• Post Reply Bookmark Topic Watch Topic
  • New Topic

Constaints during compile time  RSS feed

 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
==========
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?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tom Landry
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clarifying as that makes perfect sense.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!