• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constaints during compile time

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for clarifying as that makes perfect sense.
 
reply
    Bookmark Topic Watch Topic
  • New Topic