Static variables in non-static inner classes

First I wanted to ask Why does Java prohibit static fields in inner classes? but the question already exists. The reason I gave them to work ( serialVersionUID ) was eliminated by Bojo's answer. However, I'm still wondering:

Static fields are prohibited only in the source code or in the class file?

By the way, the compelling reason why this is prohibited is yet to be invented. C has static variables that are allowed even inside functions. Their service life is the same as that of any other static variable, only their visibility is different. The same could be used in Java.

+4
source share
3 answers

Nothing bad would happen if they were a design to allow static things. I don’t think that there are no problems at the class file level, the bytecode does not have the concept of an inner class.

This will be confusing though:

 class X class Y static int z; X x1 = new X(); XY y1 = x1.new Y(); X x2 = new X(); XY y2 = x2.new Y(); XY y3 = x2.new Y(); 

Intuitively, y2 and y3 must share the same z , but should it be the same z visible y1 ?


Conceptually, the inner class is valid only in the outer instance. Even imagine that the JVM unloads it when an external instance of GC'ed; the argument specified in [1] for class unloading is not applied, since inner classes do not have a static variable or a static initializer.

However, the reality is that there is one class shared by all internal instances, regardless of external instances. This is certainly true: y1.getClass()==y2.getClass()==y3.getClass() .

It can be argued that the language specification determines that this is true: [2]

Two types of links are the same type of runtime if ... defined by the same class loader and having the same binary name

The class y1 and class y2 have the same classloader, the binary name XY is well defined and independent of the external instance. Therefore, y1 and y2 must have the same runtime class.

If the language specification actually implied that the inner class is independent of the outer instance, the generally accepted explanation against the static state in inner classes is weakened.


[1] http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.7

[2] http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.3.4

+3
source

Inner classes can define static fields:

Inner classes cannot declare static members unless they are constant compile-time fields (Β§15.28).

Therefore, you can determine the serial version identifier.

And when the static field is not constant, then it is logically not allowed - static fields do not require an instance of the class - they are for each class, and for inner classes an instance of the owner class is required - they cannot exist without an instance. And although it may be that static fields are inner classes, they are defined as if they were in their own class, it would be more confusing.

+5
source

static members are not allowed only in the case of a non-static inner class, the non-static inner class bcz also acts as a method for the outer class, so all the rules of the methods apply to the inner class. We can declare static members in a static inner class .....

Basic rule

  • non-static inner class does not allow static methods static inner class
  • a static inner class allows everything from static to non-static
+1
source

Source: https://habr.com/ru/post/1339453/


All Articles