• 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

initializers

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Super {
Super() {
printVariable("Super");
}
public void printVariable(String name) {}
}

class Sub extends Super
{
int y;

// The following block is an instance initializer
{
printVariable("Instance Initializer(1)");
y = 10;
}

int x = 1; // Variable Initialzer
Sub() {
printVariable("Sub");
}
public void printVariable(String name) {
System.out.printf("(%s) x = %d, y = %d\n", name, x, y);
}

// The following block is an instance initializer separated by some methods
{
printVariable("Instance Initializer(2)");
y = 30;
}

public static void main(String[] args) {
new Sub();
}
}



can anyone explain the above code clearly.....
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are some key points.

1. A superclass will be initialized before a subclass.
2. The instance initializers in a class will finish before the constructor does.

So when you create a new instance of Sub, the first thing that happens is Super is initialized.

Super doesn't contain any initializers, and since Sub does not contain an explicit call to a constructor of Super, Super() is called.

Super() calls printVariable(). Since this method is overridden in Sub, the constructor of Super() will make a call to the overridden method in the subclass even though the subclass has not finished initializing.

So the first thing that prints is Super 0 0.

Next the instance initializers in Sub are executed in textual order.

The first thing that executes is a call to printVariable with the parameter Instance Initializer(1).

So Instance Initializer(1) 0 0 prints.

Remember that x and y haven't been initialized yet.

Next y is initialized to 10 and then x is initialized to 1.

Next is a call to printVariable with the parameter Instance Initializer(2).

So Instance Initializer(2) 1 0 prints.

Next y is assigned to 30.

The last thing that happens is the constructor finishes executing.

The constructor calls the method printVariable.

So the last thing that prints is Sub 1 30.
 
Get out of my mind! Look! A tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic