The program does the forward referencing of the static variables HELLO and WORLD through the method getGreeting().When this method returns all the final constants will gets its assigned value and non final references a null.

But I do also have no exaustive and correct explanation?Can anyone explains why it happens?

I think this question is about the sequence of the initialization. ok, of course the result is:hellonull if someone want to get the helloworld, follow this: public class Test { private static String WORLD = "World"; private static String GREETING = getGreeting(); private static final String HELLO = "Hello";

The Trick here is to understand sequence of initialization of members. True Constants (final static) are always assigned the value first. Then comes the static variables.

So here when the initialization of GREETING is in progress, the CONSTANT HELLO already has the value "Hello". But the VARIABLE WORLD is not initialized yet and hence holds the default value null. so is the output.

Now I made a little twist in above program and changed it to something like this.

All are static blocks here, so the order of initialization is from top to bottom. I guess this question does not test us on that. The point here is that what values will be assigned to a variable which is being used in a method before being declared. So here WORLD is being used in the method, in its return statement, but it gets declared onle after the method returns which means it is assigned the defalut value of null at run time. You can find it for yourself on the below example,

The above code prints, Hello 0, WORLD is given it's value at run time. Any value additions on this???

I think first final static variables are initialized and than static variables are initialized Thus HELLO should get initialized before GREETING & WORLD.

Now while initializing GREETING and WORLD, since GREETING comes first on the path of execution so GREETING would get initialized before WORLD.

To initialize GREETING it makes a method call to getGreetings(). In getGreetings there is a case of forward referencing for WORLD. So a default value is assigned to WORLD which is null in case of String.

Thus I guess answer should be Hellonull

Harshad Khasnis
Ranch Hand

Joined: Jun 06, 2006
Posts: 48

posted Nov 08, 2006 03:06:00

0

Hi Jothi, well let me explain the whole process of the construction of the class.

when a class is constructed, i.e. when an instance of the java.lang.Class is created for our class, at that time all the Static variables, True Constants are allocated with the memory in the JVM & initially they are assigned with default values so that these values can be used if these entities are referred before actual initialization process begins.

Next comes the initialization, here first the constants are assigned the value. then the process of initialization of the Static variables begins.

If you understand this flow then you can understand the output and in turn the behavior of the original program.

I hope I cleared your doubt.

Still I want to add that all my above statements are based on my understanding of the concepts but if I am wrong at any place above,Valentin Crettaz, Sir will you please correct me??

Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610

posted Nov 08, 2006 04:28:00

0

Good job guys, the output is "Hellonull" !! There were two key points tested in this question: 1. initialization sequence of static fields 2. initialization of constants

For the sake of discussion, I thought it would throw me a compile error because of forward referencing in

I made the same mistake. But I think, it compiles because there is this paragraph in th JLS where it says, that a declaration of a member only then has to before its use when four conditions are met.8.3.2.3, third edition And one of the conditions is: The usage is via a simple name.

And here's a method used and not a simple name. Therefore it compiles. At least so I understood this. Right or wrong?