Suppose we have code as below.
public class Outer{
class Inner{
public static final String s = "abc";
}
static class Nested{
public static final SomeOtherClass instance = new SomeOtherClass();
}
}
I understand to instantiate object of non static inner classes an object of Outer class is needed. static
means class related and for accessing it an object is not required to be instantiated. Non static inner class can only be used once we have an object of Outer class instantiated. Having any static references in it may not make sense.
My Questions:
Can non static inner class get loaded without any explicit object of Outer class ?
Why compile time constants (String literals, as they are handled in special way in String pool and primitive types) are allowed to be made static in non static inner class ?
Edit : Why can not non compile time constants be allowed to be made static, I know its as per JLS, but just wish to know what would have gone wrong, what was the intent to make this rule.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…