I wrote down this mini-program:
A
class:
public class A
{
public A()
{
System.out.println(getS());
}
public String getS() { return s;}
}
B
class:
public class B extends A
{
private String s = "hello2";
public String getS() { return s;}
}
main
:
public static void main(String[] args)
{
B b = new B();
}
and it printed:
null
Why is that?
I know that the String that printed is B's string, but why it didn't initialized before?
According to this answer - the variable initialized before the constructor..
EDIT - I edited the code so the unrelated code won't confuse