This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Order of Initialization  RSS feed

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. package testPrac;

2. public class InitializationOrder {

3. private String name = "C#.net";

4. public void disp(String name)
5. {
6. System.out.println(name);
7. }

8. public InitializationOrder() {
9. // TODO Auto-generated constructor stub
10. name = "Android!";

11. System.out.println("Constructor!");

12. }
13. {System.out.println("Instance Initialzer!");}

14. public static void main(String[] args) {
15. // TODO Auto-generated method stub

16. InitializationOrder intz = new InitializationOrder();
17. intz.disp("Java!");
18. System.out.println(intz.name);


19 }

20 }


when line 16 is executed, a new instance of the class is created and then line 3 is executed and private string variable name is assigned " C#.net ". Now instance initializer is executed which is on line 13. Now line the control comes to the constructor, here the variable name is assigned new value i.e. "Andriod" and then the print statement is executed. Now control comes to line 17 which executes the parametarized method on line 4 by passing a string value. The method takes in a string value and printout the same. Now the control is at ;ine 18 where the print statement prints the value of name variable that was assigned in the constructor.

so the output is

Instance Initializer
Constructor
Java
Android

line 17 and line 18 are interchangeable though

Though i understood the execution of the program and just wanted to know what is the order of execution. Is my understanding correct(as mentioned in the above lines and below as well)

1) fields
2) static initializer
3) Instance initializer
4) Constructor
5) Method

it this the order of execution

Thanks
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags.Otherwise it will be very difficult to help
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks sachin for pointing it out. below is the code

 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Static fields
2) Static initializers
3) Instance fields
4) Instance initializers
5) Constructor
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is the simple version. If there are several initialisers they are run in the order they are written, top to bottom.
The constructor always starts by calling another constructor. If you write this(...); then it calls another constructor in the same class. If you write super(...); then it calls the corresponding constructor in the superclass. (You cannot write both this(...); and super(...); in the same constructor.) If you don't write either it calls the superclass' no‑arguments constructor. And that constructor calls other constructors, until you get back to java.lang.Object.

You didn't write extends anywhere in your class so the constructor starts by calling the Object() constructor. Then that superclass constructor completes, and then control returns to the current constructor which completes. Then the object is available for use.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets start it from the beginning:

when you write:


class specified gets loaded,so static fields and static initializers gets loaded

Now pay full attention from here:

now when you create instance of a class by:


then all instance fields gets loaded with their default values,(i.e all instance variable of this instance as well as all instance variable of its Super class's instance)

Instance initializers are loaded,as per their occurrence(top to bottom)

now constructor of this instance is called which either calls constructor present in own class by this(...) or call the constructor of Super class by super(...),
if you neither provide this(...) or super(...) then it will call the default or nullary constructor of Super class
This will keep happening until Object class is not reached,then after it gets completed
then control comes back to constructor of Super class which was last called
and then it assigns value to instance variable of its class which is either defined explicitly,like in your case

or
if not defined explicitly then as defined inside constructor like:


Then when Super classes constructors completes then it return to your most specific class's constructor and does everything in that order as being done in Super class constructor


 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you require codes for properly understanding my post,do let me know
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain how a nullary constructor differs from a default constructor.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Default constructors are the constructor which is placed in program by compiler ,programmer has no role to play in it.

They are no parameter constructor
Whereas

Nullary constructors are the no parameter constructor which is placed by programmer itself
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Default always refer to something which is not done by programmer,and compiler does it itself

So when you are putting a constructor by yourself,you surely have no right to call it as default
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks..CR

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!