This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes final String compilation only Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "final String compilation only" Watch "final String compilation only" New topic
Author

final String compilation only

Rishabh Shah
Ranch Hand

Joined: Nov 30, 2010
Posts: 36
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

Joined: Nov 23, 2011
Posts: 45
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

Joined: Nov 30, 2010
Posts: 36
Cole Terry wrote: in a way that permanently "embeds" this value to the class Main.


means ?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Rishabh Shah
Ranch Hand

Joined: Nov 30, 2010
Posts: 36
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rishabh Shah
Ranch Hand

Joined: Nov 30, 2010
Posts: 36
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

Joined: Oct 27, 2005
Posts: 19649
    
  18

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

Joined: Nov 30, 2010
Posts: 36
Thanks A Lot

Rob Spoor
Ernest Friedman-Hill
Cole Terry


for your help ! and to make my fundamentals clear.

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7487
    
  18

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

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

Joined: Oct 13, 2005
Posts: 37890
    
  22
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

Joined: Nov 30, 2010
Posts: 36
Thanks to all of you !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final String compilation only
 
Similar Threads
Inheritance
Inlining of static final constants
Java Basics
static block and static final / non final variables
Strings