This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes rules roundup & general question 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 "rules roundup & general question" Watch "rules roundup & general question" New topic
Author

rules roundup & general question

Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
i got a question as follow:
(Q#73)Member(instance) variables are always assiogned a default value if not explicitly initialized.
i thought it was false since those member variables marked final have to be explicitly initialized......
And when the term "top level class" appears, generally, does it include toplevel nested class(static inner class)?


Meng Yi
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Here's the same question posted by me earlier. url
Lawrence Chettiar
Ranch Hand

Joined: Apr 10, 2003
Posts: 62
well Yi Meng,
This has been earlier discussed if you search you will find a long thread, any way if you see the below code

the output of the above program is
0 0
0 3
So if you see that the variable y is initiliazed but the compiler makes sure that it is initiliazed with a value in a constructer or so.
Hope it helped


SCJP 1.4 (86%)<br />SCMAD Beta (77%)<br />SCEA (part I:89%,partII:93%)<br />MCAD (cleared 70-315 & 70-320 [Score 980])
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Pay attention to these two words �initialized� and �assigned�.
The JLS says *all* instance variables in a new object are *initialized* to their default values. (JLS 12.5)
The JLS says blank final static variables must be definitely *assigned* by a static initializer and blank final instance variables must be definitely *assigned* at the end of every constructor. (JLS 8.3.1.2)
According to the JLS, just after memory is allocated for the object, the variables are *initialized*. When the variable initializers, initalizer blocks and constructors are executed, the variables are *assigned* to.
[ May 30, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I am confused.
Notice in the example above, the final variable d has the value 40 when the superclass constructor invokes print().

Notice that the final variable d is given the value 40 in the constructor of class B.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
At compile-time, d appears to be a constant. So what is the constructor B() doing?

[ May 30, 2003: Message edited by: Marlene Miller ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I can think of a reason: if the object of such class is used via reflection the compiler might not have the class to look for the constant value. A field is needed for reflection magic to access d.
In other words, if the program using d field was compiled without the presence of the class B, it cannot know the value of d, thus the compiler spawns a field, even though it might seem that it is not necessary looking at code compiled when the B class is present.


SCJP2. Please Indent your code using UBB Code
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I walked through the code in Forte. This is what is happening:
When the A() constructor is called, the final variable d is 0.
When A() invokes print(), the final variable d is still 0.
40 is printed!
print() returns.
A() returns.
The variable initializers are executed,
b is assigned 20.
d is assigned 40!
The body of B() is executed,
a is assigned 10.
c is assigned 30.
Whew! I thought for a moment I was teaching people something wrong.
So, it turns out, *all* instance variables, including final variables are initialized to their default values.
Then, the instance variable initializers (for both non-final and final variables) are executed.
Then the constructor is executed. (Just as the JLS said.)
The reason 40 is printed when the A() constructor calls print is that the compiler *hard-codes* the print statement to print 40. However, at that moment, the variable d is still 0.
[ May 30, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Yi Meng, was your question answered? Would you like a shorter, more concise answer?
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Jose,
One thing I have learned, if I can generalize from one example, is - when an instance variable is declared final and is initialized with a constant expression, (1) the compiler hard codes the constant when it is referenced (as expected) and (2) the variable is actually allocated space in the object and is assigned a value. That is what I saw in Forte.
What do you think?
Marlene
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I agree entirely with your last post and the following:

The reason 40 is printed when the A() constructor calls print is that the compiler *hard-codes* the print statement to print 40. However, at that moment, the variable d is still 0.

Running "jdb B" step by step and dumping the instance of B makes evident that the compiler is fooling us. Even after the "0 0 0 40" is printed d is 0. Only the execution of "final int d = 40;" forces the dumping of b instance to print 40 for d:

The compiler avoids that a program can observe the default value of a non-blank final.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

(1) the compiler hard codes the constant when it is referenced (as expected) and (2) the variable is actually allocated space in the object and is assigned a value.

That is what I tried to answer in my first post. Why the compiler is creating a field if it is able to produce the value for every access to the variable?. It cannot do so for the code that it does not compile .
[ May 31, 2003: Message edited by: Jose Botella ]
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
Originally posted by Marlene Miller:
Yi Meng, was your question answered? Would you like a shorter, more concise answer?

Thanks Miller, it's much more than expected....your answer really helps a lot and also Botella's.
[ May 31, 2003: Message edited by: Yi Meng ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Jose, Thank you for the jdb listing. That was very helpful.

I compiled class E separately from class D. The compiler somehow knew that x.d is a compile-time constant.
 
GeeCON Prague 2014
 
subject: rules roundup & general question