*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes why get's printed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "why get Watch "why get New topic
Author

why get's printed

vuthlarhi donald
Ranch Hand

Joined: Jul 31, 2006
Posts: 76
public class AQuestion
{
private int i = giveMeJ();
private int j = 10;

private int giveMeJ()
{
return j;
}

public static void main(String args[])
{
System.out.println((new AQuestion()).i);
}
}

why is the output 0, can anyone explain it to me
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41599
    
  55
"i" gets assigned a value before "j" is assigned one. Is this too cryptic? I don't want to be too explicit, lest I take away the opportunity for you to think this through.


Ping & DNS - my free Android networking tools app
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
hi !
I don't understand this clearly.
Is the blew describe right?

when create an object,
first,all instance variables are created and get a default value by order of the code.
next,all instance variables get the special value that the code assigned to by the order of the code.?


who dare win!<br />SCJP5(94%)|SCWCD5(86%)|SCBCD(100%)|SCEA in progress
Mohit Jain
Ranch Hand

Joined: Jun 04, 2007
Posts: 74
When method giveMeJ() returns the value of j, till then the value 10 is not yet assigned to j as the statement - private int j = 10; is not yet executed. As a result the value 0 (default int value) is taken as the value of j and is returned.
[ November 22, 2007: Message edited by: Mohit Jain ]

SCJP 5.0, SCWCD in progress
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
hi all!
Who can help me?
I just want to know,Is the blew describe right?

when create an object,
first,all instance variables are created and get a default value by order of the code.
next,all instance variables get the special value that the code assigned to by the order of the code.

I think that is the key of the problem.
when j has a default value? before i was assigned a special value giveMeJ()?

thanks in advance.
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Run the program in Eclipse in debug mode and things will be clear.
First when AQuestion is instantiated.
1) All the super class constructor runs , Steps 2-4 are repeated for super class .
2) First all the instance variables are initialized with default values in the order they are present in the code.
3) If Instance variables are initialized explicit values then they are executed.
4) Class constructor runs.

So here
1) i and j will be assigned 0.
2) i = giveMeJ() and since j is 0 , i is again assigened 0. and then j = 10.
3) Constructor runs.
So now i = 0 and j = 10.
Therefore (new AQuestion()).i will print 0 and not 10.

Now move the assignment statements i = giveMeJ() to the constructor and then you will see that i will be assigned 10 since j would have been initialized to 10 when the code in the constructor runs.
Thanks
Deepak


SCJP, SCWCD, SCBCD
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
thanks Deepak very much.
when an object is instantiated,which things will occure,you described so clearly!!
thanks!
Fu Dong Jia
Ranch Hand

Joined: May 23, 2007
Posts: 131
hi all!
Additional question:
when did the static initialization block and instance initialization block runned?

Can you insert above two events in the below list:
1) All the super class constructor runs , Steps 2-4 are repeated for super class .
2) First all the instance variables are initialized with default values in the order they are present in the code.
3) If Instance variables are initialized explicit values then they are executed.
4) Class constructor runs.
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Here is the order.
1) Static blocks of Parent class executes in the order of declaration.
2) STatic blocks of Child class executes in the order of declaration.
Once the child class is instantiated
1) First all the instance variables are initialized with default values in the order they are present in the code.
2) Instance block of Parent class executes in the order of declaration.
3) Constructos in Parent class are invoked and run and if it inhertis others then same order is repeated.
4) Super class constructors are invoked.
6) Once super class constructors finish execution. Instance blocks of child class are exected in the order of declaration.
7) If Instance variables are initialized explicit values then they are executed.
8) Class constructor runs

you can write some code and check it for yourself using Eclipse+Debug.

Thanks
Deepak
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Deepak Jain:
Here is the order.
1) Static blocks of Parent class executes in the order of declaration.
2) STatic blocks of Child class executes in the order of declaration.
Once the child class is instantiated
1) First all the instance variables are initialized with default values in the order they are present in the code.
2) Instance block of Parent class executes in the order of declaration.
3) Constructos in Parent class are invoked and run and if it inhertis others then same order is repeated.
4) Super class constructors are invoked.
6) Once super class constructors finish execution. Instance blocks of child class are exected in the order of declaration.
7) If Instance variables are initialized explicit values then they are executed.
8) Class constructor runs


Actually, #6 and #7 occur at the same level. Field initializers will be interleaved among instance initializer blocks in the order in which they all appear in the source. The same rule applies to static initializer blocks and class variable initializers (which you left out).

Example code:
The program above should print the output:

static initializer 1
class variable initializer 1
static initializer 2
class variable initializer 2
static initializer 3
class variable initializer 3
instance initializer 1
field initializer 1
instance initializer 2
field initializer 2
instance initializer 3
field initializer 3
[ November 23, 2007: Message edited by: Kelvin Lim ]

SCJP 5.0
Jan Nowak
Greenhorn

Joined: Aug 12, 2007
Posts: 16
You have to watch for other cases too (these are taken from here):




Output:
1 // variable i of I is constant and you don't have to initialize it
j=3 // j is inherited from J so J must be initialized, so jj gets initialized as well
jj=4 // but notice that k of K doesn't get initialized nor super interface of J that is I
3 //output from K.j

Also:



Output: 1729
but not: Sub 1729

a reference to a class field causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface


And here:


Output:
99
[ November 23, 2007: Message edited by: Jan Nowak ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: why get's printed