Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Constaints during compile time  RSS feed

 
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?
 
Java Cowboy
Sheriff
Posts: 16084
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!