Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
309 views
in Technique[技术] by (71.8m points)

java - Why is an anonymous class in a static context valid

I have a misunderstanding about what an anonymous class in Java is. Consider the following simple example:

public static void main (String[] args) throws java.lang.Exception
{
    B b = new B(){ };
    System.out.println(b.b);
}

interface B{ int b = 1; }

DEMO

Why does the code compile? The JLS, chapt 15 says:

An anonymous class is always an inner class (§8.1.3); it is never static

But the JLS, chapt 8

An inner class is a nested class that is not explicitly or implicitly declared static.

So the anonymous class is an inner class. But we use them in the static context. Why is it correct here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

A class can be created in a static context without being declared static, and this is what is happening here. Let's look at what being declared static, and created in a static context means:

The difference between an anonymous class created in a static context and a non-static context is whether it has an enclosing instance:

If C is an anonymous class, then:

  • If the class instance creation expression occurs in a static context, then i has no immediately enclosing instance.

  • Otherwise, the immediately enclosing instance of i is this.

A nested class that is declared static allows static members:

An inner class is a nested class that is not explicitly or implicitly declared static.

A nested class that is not an inner class may declare static members freely, in accordance with the usual rules of the Java programming language.

By saying a nested class that is 'implicity declared static', it refers to things like classes within interfaces:

A member class of an interface is implicitly static (§9.5) so is never considered to be an inner class.

Anonymous classes are not declared static (neither explicitly with a keyword, or implicitly such as being inside an interface), and so do not allow the declaration of static members. They can however be created within a static context, which means that they don't refer to an enclosing instance.

Because the anonymous classes are not declared static, both quotes in the question are consistent.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...