The only real difference is in the order of operations. Fields that are initialized in their declaration are evaluated before the constructor of the class is called. Fields that are initialized in a subclass in this manner will be evaluated after the super's constructor is completed but before the subclass's constructor is called.
Consider the following example:
I have a test class:
public class Tester {
Tester (String msg) {
System.out.println(this + ":" + msg);
}
}
I have a super class:
public class Test {
protected Tester t1 = new Tester("super init block");
Test (String constructorMsg) {
new Tester(constructorMsg);
}
}
and I have a subclass:
Public class TestSub extends Test {
private Tester t2 = new Tester("sub init block");
TestSub(String constructorMsg) {
super(constructorMsg);
new TTester("sub constructor");
}
}
In my main
method, I create an instance of TestSub
:
public static void main(String[] args) {
new TestSub("super constructor");
}
the results are as follows:
Tester@3e205f:super init block
Tester@bf73fa:super constructor
Tester@5740bb:sub init block
Tester@5ac072:sub constructor