• 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

Inherited Constructor invocation

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Can anybody explain this output of the code below to me?

The output is:
Constructor in SuperclassA
Instance initializer block in SubclassB
Instance initializer expression in SubclassB
Non-default constructor in SubclassB
Default constructor in SubclassB
value: 3

When is Instance constuctor in SuperClassA called and when is
Instance initializer block in SubclassB called and Instance initializer
expression in SubclassB called? Thanks for looking into my question.

class SuperclassA {
public SuperclassA() { // (1)
System.out.println("Constructor in SuperclassA");
}
}

class SubclassB extends SuperclassA {

SubclassB() { // (2)
this(3);
System.out.println("Default constructor in SubclassB");
}

SubclassB(int i) { // (3)
System.out.println("Non-default constructor in SubclassB");
value = i;
}

{ // (4)
System.out.println("Instance initializer block in SubclassB");
value = 2; // (5)
}

int value = initializerExpression(); // (6)

private int initializerExpression() { // (7)
System.out.println("Instance initializer expression in SubclassB");
return 1;
}
}

public class ObjectConstruction {
public static void main(String[] args) {
SubclassB objRef = new SubclassB(); // (8)
System.out.println("value: " + objRef.value);
}
}

This is code from Prof.K's book:
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the JLS, §12.5 Creation of New Class Instances:


Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:


1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.

2. If this constructor begins with an explicit constructor invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.

3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.

4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5. (In some early implementations, the compiler incorrectly omitted the code to initialize a field if the field initializer expression was a constant expression whose value was equal to the default initialization value for its type.)

5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally. In the example...

reply
    Bookmark Topic Watch Topic
  • New Topic