This week's book giveaway is in the iOS forum.
We're giving away four copies of Classic Computer Science Problems in Swift and have David Kopec on-line!
See this thread for details.
Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Do i need to compile all depependent java files if i changed a java file.  RSS feed

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
1. I have two java files A.java and B.java (B class referring some public static final variables of A class), i compiled and build jar file.
2. Due to some gap, I replaced a A.class file in existing .jar file. But the changes are not reflecting.
3. If I replaced the jar with A.class and B.class the changes are reflecting.
4. My doubt is is it really required to compile all the dependent java classes. If yes could you please explain why it is?

Thanks in Advance.
 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are using some compile time constant of A in B. what happened is compiler copies the value of a compile time constant variable to B. so to reflect a change you should recompile A.java again..
so, good practice is to not define such a constant in a constant file.

for instance,
Constants.java

in second way you no need to compile class B again. only compilation of A is enough..
it is recommended approach to design constant file as in second approach.
<edit>corrected typo</edit>
 
Gireesh Giri
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Seetharaman Venkatasamy.
But, could you please explain what it means of "compile time constant"
 
Sheriff
Posts: 11830
193
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman,

Sorry, I have to disagree with that recommendation. The way you show actually looks pretty convoluted to me and evoked an immediate "WTF?" And I don't much like classes that are purely containers of static final constants. I like to put constants in the classes with which they will most likely change. I'm not saying that global constants are completely anathema but I am usually very careful about sticking them in a class just by themselves.

@Gireesh: It's fine to want to understand what happened in this case but with proper automation, I don't see why you couldn't just recompile everything and build the jar file again. If you had done that in the first place, how much time would you have saved by not having to figure out what problem you had? Compiling and building the jar file is trivial and costs you practically nothing. It's not like you're using punch cards and teletype machines, right? Just do it and be done.

"Premature optimization is the root of all evil." -- Donald Knuth
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu, you are saying that when ever you required constant, define as private in the same file? it will lead the duplicates across files?
 
Junilu Lacar
Sheriff
Posts: 11830
193
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Seetharaman Venkatasamy wrote:Junilu, you are saying that when ever you required constant, define as private in the same file? it will lead the duplicates across files?

That's quite a leap there. If the "constant" is indeed duplicated across two or more files, then that's a smell because it violates "Don't Repeat Yourself". It's a sign that you haven't found its rightful partner class yet and you need to do more refactoring.
 
author
Sheriff
Posts: 23484
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Seetharaman Venkatasamy wrote:Junilu, you are saying that when ever you required constant, define as private in the same file? it will lead the duplicates across files?

That's quite a leap there. If the "constant" is indeed duplicated across two or more files, then that's a smell because it violates "Don't Repeat Yourself". It's a sign that you haven't found its rightful partner class yet and you need to do more refactoring.




Application design discussions aside, which is actually very interesting, what Seetharaman was referring to are compile time constants -- and the way the compiler optimizes them. The compiler does indeed "hardcode the constant" meaning there is no need to ever load it from the other class, if there is zero chance that the constant will change. And yes, the constant is "repeated", but not in source code (it's a compiler optimization). This mean that if you change a compile time constant in a class, the compiler will need to compile all dependent classes, and that you need to copy all those new classes over to reflect the changes.

Henry
 
Junilu Lacar
Sheriff
Posts: 11830
193
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not quite how I understood

Seetharaman Venkatasamy wrote:...when ever you required constant, define as private in the same file? it will lead the duplicates across files?



I understand that as: "So, when you require a constant, does that mean you define it as private in the same file that it's being used in? Doesn't that lead to duplicates across files?"

This is quite a leap from my saying "I don't like constants in their own separate file by themselves. I declare constants in the classes with which they will most likely change with." I should have said, "I usually declare..."
I'm quite happy to leave a few hard-coded values laying around in a class for the compiler to optimize in whatever way it wants behind the scenes. But it's source code that I deal with, not byte code, so when I detect a smell, I refactor it.

With the introduction of enums, there are even fewer reasons to have static final "global constants" anymore.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:
I understand that as: "So, when you require a constant, does that mean you define it as private in the same file that it's being used in? Doesn't that lead to duplicates across files?"


yes, correct.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!