Skip to content

Instantly share code, notes, and snippets.

@aploskov
Created November 7, 2018 18:37
Show Gist options
  • Save aploskov/613de0503676a2fc8a21f73801a95a17 to your computer and use it in GitHub Desktop.
Save aploskov/613de0503676a2fc8a21f73801a95a17 to your computer and use it in GitHub Desktop.
Initialization order
package com.example;
interface Hello {
void hello();
}
class A implements Hello {
static {
System.out.println("Static init A");
}
{
System.out.println("Init A");
}
A() {
System.out.println("Construct A");
}
{
System.out.println("Second init A");
}
static {
System.out.println("Second static init A");
}
public void hello() {
System.out.println("Hello A");
}
}
class B extends A {
static {
System.out.println("Static init B");
}
{
System.out.println("Init B");
}
B() {
System.out.println("Construct B");
}
{
System.out.println("Second init B");
}
static {
System.out.println("Second static init B");
}
public void hello() {
super.hello();
System.out.println("Hello B");
}
}
public class Main {
public static void main(String[] args) {
Hello b = new B();
b.hello();
}
}
@aploskov
Copy link
Author

aploskov commented Nov 7, 2018

Static init A
Second static init A
Static init B
Second static init B
Init A
Second init A
Construct A
Init B
Second init B
Construct B
Hello A
Hello B

Process finished with exit code 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment