File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final String compilation only

 
Rishabh Shah
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I have one question,

1) public class StringClass{

final String string = "Hello Rishabh !";

}

2)
public class Main{

public static void main(String[] args){
System.out.println(new StringClass().string);
}
}

There are 2 different classes. I am writing these code in command prompt.
Now, The Question is when I compile both JAVA files. and when i run it gives me output Hello Rishabh !

Now, I modify StringClass, and write

public class StringClass{
final String string="Hello CodeRanch !";
}

After changing this code...Now, I am compiling only StringClass, and when I run Main java..it gives me output Hello Rishabh!

why is it so ! I am changing only in StringClass, and compiling it only..so, why does it not give output Hello CodeRanch!

I was asked this questions yesterday, but I could not give him an answer properly..

Thanks
 
Cole Terry
Ranch Hand
Posts: 45
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is because you declared the variable "string" as an accessible field and marked it as final. The compiler knows this variable is "final" (which means it cannot be assigned to another value any more), so the compiler takes the value of "Hello Rishabh !" directly as the argument of the println() method, in a way that permanently "embeds" this value to the class Main. So you see it doesn't get changed if you re-complie the class StringClass only.
 
Rishabh Shah
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cole Terry wrote: in a way that permanently "embeds" this value to the class Main.


means ?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that the value of the variable gets hard-coded into Main; StringClass.class doesn't even need to be present when you run Main. You would need to recompile Main to get the new value.
 
Rishabh Shah
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:It means that the value of the variable gets hard-coded into Main; StringClass.class doesn't even need to be present when you run Main. You would need to recompile Main to get the new value.


You meant to say, the value of variable gets hard-coded into Main because variable is declared as final. and compiler knows that So, until we compile Main...We can't get new values..


 
Rob Spoor
Sheriff
Pie
Posts: 20369
43
Chrome Eclipse IDE Java Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. Well, mostly. It's not because it's final, it's because it's final and its content is known at compile time; it's a so-called compile time constant. In the following example there is no inlining:
Even though we both will see that this will always print "Hello Rishabh !" twice, the compiler isn't that smart.
 
Rishabh Shah
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote: there is no inlining:
System.out.println(new StringClass().string); // inlining here: yes
final String s = new StringClass().getString(); // inlining here: no

}
}


what is inlining ?
 
Rob Spoor
Sheriff
Pie
Posts: 20369
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Inlining means putting one piece of code directly inside another piece of code. In this case, the value of the constant is put in the code where it's used.

In C, even functions can be inlined; instead of the function being called each time, its body is inserted instead. If Java would allow this (which it doesn't) you would get the following:
 
Rishabh Shah
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks A Lot

Rob Spoor
Ernest Friedman-Hill
Cole Terry


for your help ! and to make my fundamentals clear.

 
Winston Gutkowski
Bartender
Pie
Posts: 9450
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:In C, even functions can be inlined; instead of the function being called each time, its body is inserted instead. If Java would allow this (which it doesn't)...

Actually, according to this page from Sun (jdk 1.4.2), it does. Exactly how (and probably more importantly when) it does it, I'm not exactly sure though.

Winston
 
Rob Spoor
Sheriff
Pie
Posts: 20369
43
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't even know that. The inlining isn't done in byte code though (I've just checked using javap -c), and you have no manual control over it.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47222
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It says in that link that the inlining is done at runtime. Of course that was for JDK1.4.2, and a lot might have changed in 8 years.
 
Rishabh Shah
Ranch Hand
Posts: 36
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all of you !
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic