I am trying to use static blocks like this:
I have a base class called Base.java
public class Base {
static public int myVar;
}
And a derived class Derived.java
:
public class Derived extends Base {
static
{
Base.myVar = 10;
}
}
My main
function is like this:
public static void main(String[] args) {
System.out.println(Derived.myVar);
System.out.println(Base.myVar);
}
This prints the out put as 0 0
where as I expected 10 0
. Can somebody explain this behavior? Also, if I want my derived classes to set the values for a static variable how can I achieve that?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…