• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

why get's printed

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.?
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Fu Dong Jia
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Fu Dong Jia
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Deepak very much.
when an object is instantiated,which things will occure,you described so clearly!!
thanks!
 
Fu Dong Jia
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 513
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic