• Post Reply Bookmark Topic Watch Topic
  • New Topic

Instance Variables Initialization??  RSS feed

 
Jignesh Gohel
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Please tell me the concept behind the flow of the code mentioned below

 
mohit bahl
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jignesh
The flow will be as follows, when the class is loaded the fields will be initalized. First i will be initialized and the function will be called where value of j is returned(and default values of i and j are printed). The default value of j is returned which is 0 and then j is initalized to 1000.
Then the constructor of the class is called which will hence print i as 0 and j as 1000 and again ias 0 and j as 100 are printed in the main function.

cheers!
Mohit
 
Jignesh Gohel
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First i will be initialized and the function will be called where value of j is returned(and default values of i and j are printed).


How does this default values comes into the picture.Constructor intitalizes all the instance variables( correct me if am wrong).But first of all the function giveMeJ() is called.And after that the constructor is called.

When a instance variable is initialized using a function how the process goes on.Who provides the default values to the variables inside that initializing function???
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohit Bahl has told you how things happen. All fields are set by the compiler to default values, which in the case of numbers are always 0 or 0.0.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jignesh Gohel:


How does this default values comes into the picture.Constructor intitalizes all the instance variables( correct me if am wrong).But first of all the function giveMeJ() is called.And after that the constructor is called.

When a instance variable is initialized using a function how the process goes on.Who provides the default values to the variables inside that initializing function???


The default field values always come into the picture, however in this case, it doesn't occur at "class load time" as suggested. You can observe a fields' initial value by invoking an overridden method from a constructor on a reference to 'this'.

Since you have been misled on this thread with misinformation, I suggest you take this premise in mind and reword your question appropriately.
 
mohit bahl
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does this default values comes into the picture.Constructor intitalizes all the instance variables( correct me if am wrong).

Yes i think you are wrong, constructor gives you the choice to initalize the instance variables to what you want. But the instance variables are always initalized to their default values. Ask yourself a question, what happens to the instance variables when you do not provide any constructor.

cheers!
Mohit
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before any initializers, variable initializers or constructor code is executed the memory for the object including it's fields must be allocated. When this happens the variables are initialized to their default values as per the JLS. It can be somewhat confusing as you can use a variable initializer, instance initializer or just an assignment in the constructor to provide an initial value for fields before the constructor returns and this is often referred to as initialization within the community. In the case of blank final fields this 'initialization' is actually mandatory and must occur in one of those three places.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!