Why can't the inner class use a static initializer?

Quoth JLS # 8.1.3 :

Inner classes cannot declare static initializers ( Β§8.7 ) ......

This is demonstrated as such:

class A { class B { static { // Compile-time Error: Cannot define static initializer in inner type AB System.out.println("Class is initializing..."); } } } 

Now, since the internal (non-static) Java classes are loaded with class loaders , like all other classes, why can't we statically initialize them?

What is the reason for this limitation?

+5
java inner-classes jls static-initializer
source share
3 answers

I think this is because the Inner class itself is not static. From a Java perspective this is an instance variable and I suppose that (1) the class loader was not intended to be traversed to internal non-static classes to search and initialize static variable objects.

But this is not an impossibility problem, look at the following example:

 public class Outer { public static class Inner { Outer owner; static String constant; { constant = "foo"; } private Inner(Outer owner) { if (owner == null) { throw new NullPointerException(); } this.owner = owner; } } public Inner newInner() { return new Inner(this); } } 

Even a warning, because Inner declared static.

But, on the other hand, it has a pointer to an Outer instance that can only be created through Outer , because it has only a private constructor, and its owner cannot be null. From the point of view of the programmer, it has all the limitations for a non-stationary inner class and can be used as one (except for special idioms like Outer.this ), but from the point of view of the compiler it is static and static fields will be correctly initialized when the Outer class is initialized.

(1): Pacerier explains below why this is not true.

0
source share

No actual use

just the opinion I came to, arguments / discussions are appreciated

Please read the topic below.

This explains: why Java forbids static fields in inner classes

IMO also applies to the static initializer for the same reason. In the end, the problem of creating a static .

In addition to the reason explained in the above thread, I can give another lame reason .
The name of the static initializer block gives us a hint about when and why to use this block. One doesn’t just use a static initializer block to print hello world (insert meme here).
The main reason for using this block is to explicitly initialize a static variable.

Now that the inner class / non-static nested class does not allow the static variable, what is the point of the static initializer?

0
source share

A contradiction by definition:

From JLS Β§8.1.3 :

An operator or expression arises in a static context if and only if the most intrinsic method, constructor, instance initializer, static initializer, field initializer, or explicit constructor invocation an operator containing an operator or expression is a static method, a static initializer, an initializer of a variable of a static variable, or explicit constructor invocation operator (Β§8.8.7).

...

When an inner class (whose declaration does not occur in a static context) refers to an instance variable that is a member of the lexically spanning class, the environment variable is used lexically to the variable.

-one
source share

All Articles