4

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:

  1. Can non static inner class get loaded without any explicit object of Outer class ?

  2. 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.

nits.kk
  • 5,204
  • 4
  • 33
  • 55

2 Answers2

5
  1. Can non static inner class get loaded without any explicit object of Outer class ?

Yes. Creating an instance of an inner class requires an instance of the outer class. But both classes can be loaded before any instances are created.

  1. Why can we have 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 ?

The language specification allows this exception for constant variables. From the Java Language Specification, section 8.1.3: "Inner classes and enclosing instances":

It is a compile-time error if an inner class declares a member that is explicitly or implicitly static, unless the member is a constant variable (§4.12.4).

And String variables can be constants, because of section 4.12.4, "final Variables":

A constant variable is a final variable of primitive type or type String that is initialized with a constant expression (§15.28).

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
  • Thanks Andy for the answer, I have edited my question : Why can not non 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. – nits.kk May 29 '16 at 13:54
  • 1
    @nits.kk - For your third question, I don't have a definitive answer. It's been asked before on this site and elsewhere. All I've seen is speculation or misunderstanding. For example, see ["Why does Java prohibit static fields in inner classes?"](http://stackoverflow.com/questions/1953530/why-does-java-prohibit-static-fields-in-inner-classes). – Andy Thomas May 29 '16 at 14:16
  • What do you mean by "loaded" in the sentense "But both classes can be loaded before any instances are created.". Is it different from "created"? As per [docs](https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html)=, we can create inner class instance only from outer class instance: `OuterClass.InnerClass innerObject = outerObject.new InnerClass();` – MsA Oct 28 '18 at 13:55
  • @anir - Before a class can be used, it has to be [*loaded* by the virtual machine](https://en.wikipedia.org/wiki/Java_Classloader). After it's loaded, multiple instances can be *created*. – Andy Thomas Oct 29 '18 at 14:46
0

Question 1 - Can non static inner class get loaded without any explicit object of Outer class ?

Yes, you can load non-static inner class that way, look sample below:

class MyObject {
    class InnerObject {
        static final String prop = "SOME INNER VALUE";
    }

    static void doLoad() {
        System.out.println(InnerObject.prop);
    }
}

The thing is there are not many reasons to do so, because non-static inner class is not allowed to have any static blocks, methods, fields if those are not final as in a question below.

Question 2 - Why can we have 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 ?

Java is designed so you can use static, final fields in non-static inner classes.

gevorg
  • 4,835
  • 4
  • 35
  • 52
  • I think you answered the wrong question for q1. OP is asking about *non*-static inner classes, not about *static* inner classes. – Klitos Kyriacou May 29 '16 at 13:36