A simple rule is direct or indirect access to the "this" object from the constructor.
This means that you should not call overridden methods from the constructor, and you should not call a method that calls an overridable method, or call a method that calls a method that calls an overridable method, or ... you get an idea.
, "this" -, .
. :
class Bar extends Foo
{
public Bar () {
super (doSmth(this));
...
}
private static String doSmth (Bar bar) {
}
}
() , doSmth overriden Bar, , .
, :
public class Main
{
public static void main(final String[] argv)
{
final A a;
a = new B();
a.foo();
}
}
abstract class A
{
protected A()
{
bar(this);
}
private static void bar(final A a)
{
a.foo();
}
public abstract void foo();
}
class B extends A
{
final String str;
public B()
{
super();
str = "Hello, World!";
}
public void foo()
{
System.out.println("str - " + str);
}
}
, , - , . "" ( ) , , .