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.
Joined: Nov 30, 2010
Cole Terry wrote: in a way that permanently "embeds" this value to the class Main.
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..
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.