• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

SCJP Brainteaser (6)

 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hellonull

Is this because the HELLO is a true constanst (final and static) whilst WORLD is a static?
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would say final variable not initialized.
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Valentin,

I had a similar Question posted here. Static Variables


All your brainteasers are good.

reply
    Bookmark Topic Watch Topic
  • New Topic