• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Inheritance

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When we're creating the object of a sub class is it that the super class is loaded automatically?
Why is the Instance block of Sub processed before the Static block?

The output is:

Static block in Super
Instance block in Super
Instance block in Sub
Static block in Sub
Main in Sub
 
Ranch Hand
Posts: 1283
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch.. you'll get more response, if you'll move your in correct forum. Here if you see in your super static block you're creating an instance on sub class, I think that's the reason you're getting below result.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Firoj,

If you want to exectue super class first the following format

class Super {
static {
System.out.println("Static Block in Super");
}
{
System.out.println("Instance Block in Super");
}
}
class Sub extends Super{
{
System.out.println("Instance block in Sub");
}
static {
System.out.println("Static block in Sub");
}
public static void main(String []arg){
Super s = new Super();
System.out.println("Main in Sub");
}

}

Output:

Static Block in Super
Static block in Sub
Instance Block in Super
Main in Sub



Otherwise you want to exectue sub class first the following format

class Super {
static {
System.out.println("Static Block in Super");
}
{
System.out.println("Instance Block in Super");
}
}
class Sub extends Super{
{
System.out.println("Instance block in Sub");
}
static {
System.out.println("Static block in Sub");
}
public static void main(String []arg){
Sub s = new Sub ();
System.out.println("Main in Sub");
}

}

Output:

Static Block in Super
Static block in Sub
Instance Block in Super
Instance block in Sub
Main in Sub


Thanks&Regards,
Dhaya
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure why you are using static initializers with this code, because there is an easier way to show what you're seeing here. The simple fact is that when a subclass constructor is called it implicitly calls or invokes its superclass constructor all the way up the Java class hierarchy. That is the reason you are seeing the super class one before the subclass being printed. The static initializers, on the other hand, are called when the class is loaded. This is done in sequence, unless some other syntax modifies the flow of control. Apart from the constructor chaining, I couldn't tell you why you're getting the output you are getting. I wrote code similar to yours and I couldn't get it to compile because it doesn't like the static block declaration in the inner class. If you haven't already, copy the code exactly from your program into the thread.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic