Is there any way to have a static initializer in a parent class executed when I instantiate a sub-class?

Luigi Viggiano

 

 

The static initializer is executed only when the arent class gets loaded.


class Parent {
  static { //static initializer
    System.out.println("executed");
  }
}

class Child1 extends Parent {
}

class Child2 extends Parent {
}

public class Main {
    public static void main(String[] args) {
        new Child1();
        new Child2();
    }
}
In the previous example the output is:
executed
only one time, because Parent class is only loaded once. When you instantiate Child1 or Child2, the parent class is loaded (the first time only), and kept in a memory.

If you want that code executed every time Child1 or Child2 is instantiated then you have to replace the static initializer with an instance initializer:  


class Parent {
  { //instance initializer
    System.out.println("executed");
  }
}

class Child1 extends Parent {
}

class Child2 extends Parent {
}

public class Main {
    public static void main(String[] args) {
        new Child1();
        new Child2();
    }
}

The output this time will be:
executed
executed

Static initializers and instance initializers are executed before any constructor, and not when constructor or super() is called.
Everyting static belongs to a class, everything is not static belongs to the object (or instance). Then a static initializer is executed when class is loaded, and an instance initializer when a new object is instantiated.

Here's a good example on what happens with static & instance initializers, & constuctors:  


class Parent {
    static {
	System.out.println("Parent static initializer executed...");
    } 

    {
	System.out.println("Parent instance static initializer executed...");
    } 

    Parent() {
	System.out.println("Parent constructor executed");
    }

}

class Child1 extends Parent {
    {
	System.out.println("Child1 instance  initializer executed...");
    } 

    static {
	System.out.println("Child1 static initializer executed...");
    } 

    Child1() {

	// super() is called implicitly
	System.out.println("Child1 constructor executed");
    }

}

class Child2 extends Parent {
    static {
	System.out.println("Child2 static initializer executed...");
    } 

    {
	System.out.println("Child2 instance initializer executed...");
    } 

    Child2() {
	super();    // called explicitly

	System.out.println("Child2 constructor executed");
    }

}

public class Main {
    static {
	System.out.println("Main static initializer executed...");
    } 

    {
	System.out.println("Main instance initializer executed...");
    } 

    Main() {
	System.out.println("Main constructor initializer executed...");
    }

    public static void main(String[] args) {
	System.out.println("main method starting...");
	new Child1();
	System.out.println("Child1 finished...");
	new Child2();
	System.out.println("Child2 finished...");
    } 
}

the output will be:

 


Main static initializer executed...
main method starting...
Parent static initializer executed...
Child1 static initializer executed...
Parent instance static initializer executed...
Parent constructor executed
Child1 instance  initializer executed...
Child1 constructor executed
Child1 finished...
Child2 static initializer executed...
Parent instance static initializer executed...
Parent constructor executed
Child2 instance initializer executed...
Child2 constructor executed
Child2 finished...
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact