wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP Brainteaser (6) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP Brainteaser (6)" Watch "SCJP Brainteaser (6)" New topic
Author

SCJP Brainteaser (6)

Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Alright guys! Next one...

What happens when the following code is compiled and run?



Note that the exercise will be most beneficial to you if you don't throw the code in a compiler. Instead, you should compile and run the code mentally


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Hi Above,

It prints Hellonull. It is an esample of forward referencing. By the time the method returns, the compile time constant Hello is given to HELLO and WORLD has null.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
For the sake of discussion, I thought it would throw me
a compile error because of forward referencing in



it doesn't, I won't tell you the result.


"Eppur si muove!"
Jae Stryker
Greenhorn

Joined: Oct 31, 2006
Posts: 21
Hellonull

Is this because the HELLO is a true constanst (final and static) whilst WORLD is a static?
Sanjeev Singh
Ranch Hand

Joined: Nov 01, 2006
Posts: 381
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?


~Sanjeev Singh<br />SCJP 1.5
Taihua Yu
Greenhorn

Joined: Oct 27, 2006
Posts: 7
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";

public static String getGreeting() {
return HELLO + WORLD;
}

public static void main(String[] args) {
System.out.println(GREETING);
}
}
[ November 08, 2006: Message edited by: Taihua Yu ]
Harshad Khasnis
Ranch Hand

Joined: Jun 06, 2006
Posts: 48
OK the output is Hellonull.

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.



Anybody for comments on the output this one.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

It would say final variable not initialized.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Hi Harshad,

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???
Saurabh Vyas
Ranch Hand

Joined: Sep 02, 2003
Posts: 72
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
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
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

The explanations given so far are quite accurate. For those who want to get some bedtime reading, here are the two relevant JLS sections:
8.3.2.3 Restrictions on the use of Fields during Initialization
12.4.2 Detailed Initialization Procedure (Bullet 8)

Stay tuned...
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

Gian Franco Casula posted November 08, 2006 12:36 AM
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?

Yours,
Bu.


all events occur in real time
Sunil Kumar Jakkaraju
Greenhorn

Joined: Oct 06, 2006
Posts: 24
Valentin,

I had a similar Question posted here. Static Variables


All your brainteasers are good.



Thanks & regards,
Sunil
When all else fails, follow instructions. - Allen's Axiom
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SCJP Brainteaser (6)
 
Similar Threads
JBoss5.1 EJB3 JSF1.2 integration problem
Space character in URL error when running midlet
overridding private methods
Hello world spring aop
static members