Java-匿名类是否为静态 [英] Java - Anonymous class are static or not

查看:87
本文介绍了Java-匿名类是否为静态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I know it depends on the context in which the anonymous class has been written (static or non static method). but look this part of code:

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?
            }
        };
    }
}

确定匿名类是静态的吗?

it's sure that anonymous class are static?

推荐答案

如果上下文是静态的,则匿名类是静态的.例如用静态方法.

An anonymous class is static if the context is static. e.g. in a static method.

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

An anonymous class is non static if there is a non static context, whether you need it to be non-static or not. The compiler is not smart enough to make a class static if the non static context is not used.

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

In this example, two anonymous classes were created. One in a static method has no reference to an outer class and is like a static nested class.

注意:这些类仍称为内部",即使它们没有引用外部类,也不能具有静态成员.

Note: these classes are still called "Inner" and cannot have static members even though they have no reference to an Outer class.

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 []

这篇关于Java-匿名类是否为静态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆