This week's book giveaway is in the General Computing forum.
We're giving away four copies of Emmy in the Key of Code and have Aimee Lucido on-line!
See this thread for details.
Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

constructor initialize order

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
below is a code which i picked up from this forum only.
i yet cant understand the order in which the constructors r
called .
can anyone xplain step step the control flow of the program pls.
thxa lot
class superclassa
{
protected int supervalue;
protected int i=10;
public superclassa()
{
System.out.println("constructor in superclass");
this.dovalue();
System.out.println(this.i);
}
public void dovalue()
{
this.supervalue=911;
System.out.println("supervalue:"+this.supervalue);
}
}
class subclassb extends superclassa
{
private int value=800;

public subclassb()
{
System.out.println("constructor in subclassb");
i=30;
this.dovalue();
System.out.println("supervalue:"+this.supervalue);
}

public void dovalue()
{
System.out.println("value:"+this.value);
}
}
public class object1
{
public static void main(String[]args)
{
subclassb b=new subclassb();
superclassa a=new superclassa();
System.out.println(b.i);
}
}
OUTPUT BELOW:::
constructor in superclass
value:0
10
constructor in subclassb
value:800
supervalue:0
constructor in superclass//line number 49.
supervalue:911
10
30
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Whenever an object is created with the "new" keyword,a call is made to the constructor(Its own).
If in constructor there is any explicit call to constructor of same class (with this()) or to the constructor of superclass(with super()), that will execute
but in absence of both if the object has any superclasses,then a implicit call is immediately made to the superclass default constructor and this goes on until we go all the way up the inheritance hierarchy.
Say ,there is just one superclass,then when the superclass constructor is called.
Execution goes in this way :
a)if the superclass has any initalizer block,then that is executed first and then the constructor is executed.
b)The initializer block in the subclass is executed and then the subclass constructor is executed.
2)If the Superclass itself has some static initializers then that is executed first before everything else (i.e. even before the static initializers of the subclass) and then the static initializers of the subclass are executed,followed by Initializers and so on and so forth.......
So the order can be put as follows:
Superclass:Static Decl
Superclass:Static Block
Subclass:Static Decl
Subclass:Static Block
Superclass:Initializer
Superclass:Constructor
Subclass:Initializer
Subclass:Constructor
 
kamal jaisingh
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thx for ur reply , but i yet a little confused abt the
below code.
can u xplain me the flow of the below code.
thx a lot
regards

class superclassa
{
protected int supervalue;
protected int i=10;
public superclassa()
{
System.out.println("constructor in superclass");
this.dovalue();
System.out.println(this.i);
}
public void dovalue()
{
this.supervalue=911;
System.out.println("supervalue:"+this.supervalue);
}
}
class subclassb extends superclassa
{
private int value=800;
public subclassb()
{
System.out.println("constructor in subclassb");
i=30;
this.dovalue();
System.out.println("supervalue:"+this.supervalue);
}

public void dovalue()
{
System.out.println("value:"+this.value);
}
}
public class object1
{
public static void main(String[]args)
{
subclassb b=new subclassb();
superclassa a=new superclassa();
System.out.println(b.i);
}
}
OUTPUT BELOW:::
constructor in superclass
value:0
10
constructor in subclassb
value:800
supervalue:0
constructor in superclass//line number 49.
supervalue:911
10
30
Kamal j
 
Ishaan Mohan
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kamal,
subclassb b=new subclassb();
At this line you are creating object of subclassb
1. First execution will transfer to constructor of subclassb(Not execute)
2. As there is no explicit call to any sonstructor form constructor of subclassb, compiler will make implicit call to superclass constructor.
3. Superclass constructor exceuted.(Also initialization of var.)
protected int supervalue; ( to 0)
protected int i=10; ( to 10)
public superclassa()
{
System.out.println("constructor in superclass");
this.dovalue();
System.out.println(this.i);
}
4. Message "Constructor in superclass " will print on screen.
5. this.dovalue() will call dovalue() fuction of subclass b as you have created object of this class.
6. Execution is transfer to subclassb function.
public void dovalue()
{
System.out.println("value:"+this.value);
}
7. As Variables of subclassb are not initialized yet so you will get 0 on screen.
8. Execution is transfer back to Superclass Constructor
System.out.println(this.i);
9. 10 will be printed.
10. Execution is transfer to subclassb constructor(Var. Initialised)
11. Message :Constructor in Subclassb" printed.
12. dovalue() method of subclass is called.
13. System.out.println("value:"+this.value);
prints value=800(as this time var. in subclass are intialized)
14.System.out.println("supervalue:"+this.supervalue); prints default value of supervalue as 0 becoz dovalue() function of superclass is not called yet so value is not assigned.
15. Next line in main function :
superclassa a=new superclassa();
16. Super class constructor get executed.
17. dovalue() function of super is called as you have created object of superclass.
18. this.supervalue=911;
System.out.println("supervalue:"+this.supervalue);
Prints 911
19.System.out.println(this.i); Prints 10
20. System.out.println(b.i); Prints 30( value of i in subclass)
I tried my best but if you have some problem I will try again
 
Screaming fools! It's nothing more than a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!