Order of Constructors
& Initializers

Nicholas Duchon: Oct 4, 2015.

Outline


One

// file: Order001.java
// date: Oct 4, 2015
// Author: Nicholas Duchon
// Purpose: demo constructor order calls

public class Order001 {

  public Order001 () {
    System.out.println ("In no parameter constructor: Order001");
  } // end no paramater constructor

  public static void main (String args []) {
    Order001 x = new Order001();
  } // end main

} //end class Order001

Comments

  1. Just a start
  2. Instantiating class in main
  3. Calls the no parameter constructor
  4. All other defaults do nothing here

Output:

In no parameter constructor: Order001

Two

// file: Order002.java
// date: Oct 4, 2015
// Author: Nicholas Duchon
// Purpose: demo constructor order calls

public class Order002 {

  public Order002 () {
    this ("a string");
    System.out.println ("In no parameter constructor: Order002");
  } // end no paramater constructor
 
  public Order002 (String s) {
    System.out.println ("In String constructor: " + s);
  } // end String constructor

  public static void main (String args []) {
    Order002 x = new Order002();
  } // end main

} //end class Order002

Comments

  1. Use this reference to call another constructor
  2. Notice the order:
    1. this constructor first
    2. THEN no parameter constructor body

Output:

In String constructor: a string
In no parameter constructor: Order002


Three

// file: Order003.java
// date: Oct 4, 2015
// Author: Nicholas Duchon
// Purpose: demo constructor order calls

public class Order003 {

  static {
    System.out.println ("Order003 static initializer");
  } // static initializer

  public Order003 () {
    this ("a string");
    System.out.println ("In no parameter constructor: Order003");
  } // end no paramater constructor
 
  public Order003 (String s) {
    System.out.println ("In String constructor: " + s);
  } // end String constructor

  public static void main (String args []) {
    Order003 x = new Order003();
  } // end main

} //end class Order003

Comments

  1. Added a static initializer
  2. static initializer happens before either constructor

Output:

Order003 static initializer
In String constructor: a string
In no parameter constructor: Order003

Four

// file: Order004.java
// date: Oct 4, 2015
// Author: Nicholas Duchon
// Purpose: demo constructor order calls

public class Order004 {

  static {
    System.out.println ("Order004 static initializer");
  } // static initializer

  {
    System.out.println ("\nOrder004 instance initializer");
  } // static initializer

  public Order004 () {
    this ("a string");
    System.out.println ("In no parameter constructor: Order004");
  } // end no paramater constructor
 
  public Order004 (String s) {
    System.out.println ("In String constructor: " + s);
  } // end String constructor

  public static void main (String args []) {
    Order004 x = new Order004();
    Order004 y = new Order004();
  } // end main

} //end class Order004

Comments

  1. Made TWO instances of this class in main
  2. Added instance (not static) initializer
  3. static initializer happens only once
  4. instance initializer happens before any constructor code

Output:

Order004 static initializer

Order004 instance initializer
In String constructor: a string
In no parameter constructor: Order004

Order004 instance initializer
In String constructor: a string
In no parameter constructor: Order004

Five

// file: Order005.java
// date: Oct 4, 2015
// Author: Nicholas Duchon
// Purpose: demo constructor order calls

public class Order005 {
  Order005a z = new Order005a (23);

  static {
    System.out.println ("Order005 static initializer");
  } // static initializer

  {
    System.out.println ("Order005 instance initializer");
  } // static initializer

  public Order005 () {
    this ("a string");
    System.out.println ("In no parameter constructor: Order005");
  } // end no paramater constructor
 
  public Order005 (String s) {
    System.out.println ("In String constructor: " + s);
  } // end String constructor

  public static void main (String args []) {
    Order005 x = new Order005();
    Order005 y = new Order005();
  } // end main

} //end class Order005

class Order005a {
  public Order005a (int a) {
    System.out.println ("\nIn int constructor Order005a: " + a);
  } // end int constructor
} // end class Order005a

Comments

  1. Added another class
    1. class has an int parameter constructor
  2. The new class is instantiated as a instance variable in main class
  3. Now still static initializer first
  4. Then instance variable creation and initialization through Order005a constructor
  5. Then instance initializer
  6. Then constructors

Output:

Order005 static initializer

In int constructor Order005a: 23
Order005 instance initializer
In String constructor: a string
In no parameter constructor: Order005

In int constructor Order005a: 23
Order005 instance initializer
In String constructor: a string
In no parameter constructor: Order005

Six

// file: Order006.java
// date: Oct 4, 2015
// Author: Nicholas Duchon
// Purpose: demo constructor order calls

public class Order006 {
  Order006a z = new Order006a (23);

  static {
    System.out.println ("Order006 static initializer");
  } // static initializer

  {
    System.out.println ("Order006 instance initializer");
  } // static initializer

  public Order006 () {
    this ("a string");
    System.out.println ("In no parameter constructor: Order006");
  } // end no paramater constructor
 
  public Order006 (String s) {
    System.out.println ("In String constructor: " + s);
  } // end String constructor

  public static void main (String args []) {
    Order006  ma = new Order006();
    Order006  mb = new Order006();
    Order006b mc = new Order006b();
  } // end main

} //end class Order006

class Order006a {
  public Order006a (int a) {
    System.out.println ("\nIn int constructor Order006a: " + a);
  } // end int constructor
} // end class Order006a

class Order006b extends Order006 {
  public Order006b () {
    System.out.println ("In no parameter constructor: child class Order006b");
  } // end no parameter constructor
} // end child class Order006b

Comments

  1. Another class, Order006b extends Order006
  2. How does a parent/child constructor sequence work?
  3. main instantiates first two instances of Order006 as before
  4. Sequence for instantiating Order006b:
    1. begin with parent instance variable Order006.z
    2. parent initializer
    3. parent String constructor
    4. parent no parameter constructor
    5. child constructor

Output:

Order006 static initializer

In int constructor Order006a: 23
Order006 instance initializer
In String constructor: a string
In no parameter constructor: Order006

In int constructor Order006a: 23
Order006 instance initializer
In String constructor: a string
In no parameter constructor: Order006

In int constructor Order006a: 23
Order006 instance initializer
In String constructor: a string
In no parameter constructor: Order006
In no parameter constructor: child class Order006b

Seven

// file: Order007.java
// date: Oct 4, 2015
// Author: Nicholas Duchon
// Purpose: demo constructor order calls

public class Order007 {
  Order007a z = new Order007a (23);

  static {
    System.out.println ("Order007 static initializer");
  } // static initializer

  {
    System.out.println ("Order007 instance initializer");
  } // static initializer

  public Order007 () {
    this ("a string");
    System.out.println ("In no parameter constructor: Order007");
  } // end no paramater constructor
 
  public Order007 (String s) {
    System.out.println ("In String constructor: " + s);
  } // end String constructor

  public static void main (String args []) {
    System.out.println ("\nCreating Order007: "); new Order007();
    System.out.println ("\nCreating Order007: "); new Order007();
    System.out.println ("\nCreating Order007b: "); new Order007b();
  } // end main

} //end class Order007

class Order007a {
  public Order007a (int a) {
    System.out.println ("In int constructor Order007a: " + a);
  } // end int constructor
} // end class Order007a

class Order007b extends Order007 {
  Order007a z2 = new Order007a (2345);
 
  public Order007b () {
    System.out.println ("In no parameter constructor: child class Order007b");
  } // end no parameter constructor
} // end child class Order007b

Comments

  1. Added an instance variable to child class
  2. order:
    1. parent instance variable (23)
    2. parent initializer
    3. parent String constructor (using this)
    4. parent no parameter constructor
    5. child instance variable (2345)
    6. child no parameter constructor

Output:

Order007 static initializer

Creating Order007:
In int constructor Order007a: 23
Order007 instance initializer
In String constructor: a string
In no parameter constructor: Order007

Creating Order007:
In int constructor Order007a: 23
Order007 instance initializer
In String constructor: a string
In no parameter constructor: Order007

Creating Order007b:
In int constructor Order007a: 23
Order007 instance initializer
In String constructor: a string
In no parameter constructor: Order007
In int constructor Order007a: 2345
In no parameter constructor: child class Order007b

Eight

// file: Order008.java
// date: Oct 4, 2015
// Author: Nicholas Duchon
// Purpose: demo constructor order calls

public class Order008 {
  Order008a z = new Order008a (23);

  static {
    System.out.println ("Order008 static initializer");
  } // static initializer

  {
    System.out.println ("Order008 instance initializer");
  } // static initializer

  private Order008 () {
    this ("a string");
    System.out.println ("In no parameter constructor: Order008");
  } // end no paramater constructor
 
  public Order008 (String s) {
    System.out.println ("In String constructor: " + s);
  } // end String constructor

  public static void main (String args []) {
    System.out.println ("\nCreating Order008: "); new Order008();
    System.out.println ("\nCreating Order008: "); new Order008();
    System.out.println ("\nCreating Order008b: "); new Order008b();
  } // end main

} //end class Order008

class Order008a {
  public Order008a (int a) {
    System.out.println ("In int constructor Order008a: " + a);
  } // end int constructor
} // end class Order008a

class Order008b extends Order008 {
  Order008a z2 = new Order008a (2345);
 
// NOTE: Compiler error:
//       Following ILLEGAL because Order008 no parameter constructor
//       is private, hence hidden from the IMPLICIT super () call
//       first line of the following constructor

//   public Order008b () {
//     System.out.println ("In no parameter constructor: child class Order008b");
//   } // end no parameter constructor

// NOTE: the following is LEGAL because of the EXPLICIT super (String)
//       call in the first line of this constructor to the public
//       String constructor in Order008

  public Order008b () {
    super ("call to String constructor from Order008b");
    System.out.println ("In no parameter constructor: child class Order008b");
  } // end no parameter constructor

} // end child class Order008b

Comments

  1. As before, but this code makes the parent no parameter constructor PRIVATE
  2. Now the code in magenta comments causes a compiler error
  3. Because this code makes makes a default reference
    to the parent no parameter constructor
  4. Note that a super (String) is OK since
    that constructor is still public.

Output:

Order008 static initializer

Creating Order008:
In int constructor Order008a: 23
Order008 instance initializer
In String constructor: a string
In no parameter constructor: Order008

Creating Order008:
In int constructor Order008a: 23
Order008 instance initializer
In String constructor: a string
In no parameter constructor: Order008

Creating Order008b:
In int constructor Order008a: 23
Order008 instance initializer
In String constructor: call to String constructor from Order008b
In int constructor Order008a: 2345
In no parameter constructor: child class Order008b


End for now.



End
Nicholas Duchon
Oct 4, 2015