【问题标题】:Java - Anonymous class are static or notJava - 匿名类是否是静态的
【发布时间】:2013-12-12 21:44:18
【问题描述】:

我知道这取决于编写匿名类的上下文(静态或非静态方法)。 但是看看这部分代码:

public class A {
    int fieldOfA;

    private static class B {
        int fieldOfB;
    }

    public static void main(String[] args) {

        B obj = new B() { //this anonymous class is static becuase is in the main method.

            private static void testMethod() { //so why here i have an error and i can put just a non-static method
                                               //if my class is static ?
                                               //a class static can have static method, but this class no, why?
            }
        };
    }
}

确定匿名类是静态的吗?

【问题讨论】:

  • 该类不是静态的,它是主函数的本地类。你在哪里找到这个定义的?
  • 我们已经在 4 天前进行了这场战斗。
  • 静态类是用'static'关键字显式标记的。
  • 并且标有static关键字的类是静态的。

标签: java static-class


【解决方案1】:

如果上下文是静态的,则匿名类是静态的。例如在静态方法中。

如果存在非静态上下文,则匿名类是非静态的,无论您是否需要它是非静态的。如果不使用非静态上下文,编译器不够聪明,无法将类设为静态。

在此示例中,创建了两个匿名类。静态方法中的一个没有对外部类的引用,就像一个静态嵌套类。

注意:这些类仍然称为“Inner”,即使它们没有引用 Outer 类,也不能有静态成员。

import java.util.Arrays;

public class Main {
    Object o = new Object() {
        {
            Object m = Main.this; // o has a reference to an outer class.
        }
    };

    static Object O = new Object() {
        // no reference to Main.this;
        // doesn't compile if you use Math.this
    };

    public void nonStaticMethod() {
        Object o = new Object() {
            {
                Object m = Main.this; // o has a reference to an outer class.
            }
        };
        printFields("Anonymous class in nonStaticMethod", o);
    }

    public static void staticMethod() {
        Object o = new Object() {
            // no reference to Main.this;
            // doesn't compile if you use Math.this
        };
        printFields("Anonymous class in staticMethod", o);
    }

    private static void printFields(String s, Object o) {
        System.out.println(s + " has fields " + Arrays.toString(o.getClass().getDeclaredFields()));
    }

    public static void main(String... ignored) {
        printFields("Non static field ", new Main().o);
        printFields("static field ", Main.O);
        new Main().nonStaticMethod();
        Main.staticMethod();
    }
}

打印

Non static field  has fields [final Main Main$1.this$0]
static field  has fields []
Anonymous class in nonStaticMethod has fields [final Main Main$3.this$0]
Anonymous class in staticMethod has fields []

【讨论】:

  • @Peter 对不起,我不明白你的回答
  • JLS 不同意这一点。
  • @GiovanniFar 我添加了四个示例,其中两个匿名类在静态上下文中定义并且没有引用外部类实例,以及两个匿名类,其中该类隐式引用了外部类的实例。
  • 示例还可以,但较小的 o 和较大的 O 会妨碍您轻松理解。 :(
【解决方案2】:

来自JLS 15.9.5

匿名类始终是内部类(第 8.1.3 节);它永远不是静态的(§8.1.1、§8.5.1)。

8.1.3 部分更多地讨论了内部类,包括它们何时出现在静态上下文中。但它们本身从来都不是静态的,因此不能声明静态成员(常量变量除外)。

【讨论】:

  • 这是我的意思,那么为什么有些人说这取决于匿名类在哪个上下文中?
  • @GiovanniFar 我刚刚编辑了我的答案。如果匿名类在静态上下文中声明(见 8.1.3),则匿名类中的语句和表达式可能具有静态上下文。但是匿名类本身从来都不是静态的。有些人可能会说不同,但他们错了,因为 JLS 从定义上讲是正确的。 :)
猜你喜欢
  • 2010-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多