This has exactly the same result, but you should never do this: This is difficult to read and most Java Engineers don’t even know how this works. Then why did I decide to write an article about this if it’s a “worst practice”? Well, it can be fun to know about tricks in Java that others don’t. But more importantly, there is something to learn here. What is this doing? How does it work? We are going to learn that and at the very end I’ll show you what code you should be using instead that’s the best of both worlds.

Before I explain what’s going on here, I’ve got to explain a little known concept called “Instance Initializers”. Did you know you could do this:

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

packagecom.sleepeasysoftware.dontdothis;

/**

* Created by Daniel Kaplan on behalf of Sleep Easy Software.

*/

publicclassTheBreakdown{

static{

System.out.println("I run first");

}

publicstaticvoidmain(String[]args){

newTheBreakdown();

}

{//This is an Instance Initializer

System.out.println("I run second");

}

publicTheBreakdown(){

System.out.println("I run third");

}

}

As you would expect from the way I labeled these
String s, this outputs:

Java

1

2

3

I run first

I run second

I run third

The Instance Initializer gets run before any constructor is run, each time you initialize this class. If you’ve never heard of Instance Initializers, it’s probably because there isn’t much use for them. The most useful thing you can do seems to be that it lets you execute code when an anonymous inner class is created. Anonymous inner classes don’t have names so you can’t define a constructor for them. But you can do this:

I still wouldn’t recommend this, because — like I said — most Java developers won’t understand what’s going on here. Here’s an even trickier example:

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

packagecom.sleepeasysoftware.dontdothis;

/**

* Created by Daniel Kaplan on behalf of Sleep Easy Software.

*/

publicclassThisMakesMyBrainHurt{

publicstaticvoidmain(String[]args){

newChild();//this constructor calls Parent()

newChild("bye");//this constructor calls Parent(String)

}

staticclassParent{

privateStringsomething=null;

publicParent(){

something="hi";

}

publicParent(Stringparam){

something=param;

}

publicvoidsaySomething(){

System.out.println(something);

}

}

staticclassChildextendsParent{

{

saySomething();

}

publicChild(){

super();

}

publicChild(StringchildParam){

super(childParam);

}

}

}

The output of this is

Java

1

2

hi

bye

This is evidence that the
super in the
Child Constructor is called, then the
Child Instance Initializer, then the rest of the
Child Constructor is executed. These are some really esoteric details that you’d probably never run into in real life. So if this part is confusing, I think you can ignore it.