简体   繁体   English

为什么我们可以拥有静态最终成员但是在内部类中不能使用静态方法?

[英]Why can we have static final members but cant have static method in an inner class?

Why can we have static final members but cant have static method in an non static inner class ? 为什么我们可以拥有静态最终成员但不能在非静态内部类中使用静态方法?

Can we access static final member variables of inner class outside the outer class without instantiating inner class ? 我们可以访问外部类之外的内部类的静态最终成员变量而无需实例化内部类吗?

YOU CAN have static method in a static "inner" class. 您可以在静态 “内部”类中使用静态方法。

public class Outer {
    static String world() {
        return "world!";
    }
    static class Inner {
        static String helloWorld() {
            return "Hello " + Outer.world();
        }
    }   
    public static void main(String args[]) {
        System.out.println(Outer.Inner.helloWorld());
        // prints "Hello world!"
    }
}

To be precise, however, Inner is called a nested class according to JLS terminology ( 8.1.3 ): 但是,确切地说,根据JLS术语( 8.1.3 ), Inner被称为嵌套类:

Inner classes may inherit static members that are not compile-time constants even though they may not declare them. 内部类可以继承非编译时常量的静态成员,即使它们可能不会声明它们。 Nested classes that are not inner classes may declare static members freely, in accordance with the usual rules of the Java programming language. 不是内部类的嵌套类可以根据Java编程语言的通常规则自由地声明静态成员。


Also, it's NOT exactly true that an inner class can have static final members; 而且,内部类可以拥有static final成员并不完全正确; to be more precise, they also have to be compile-time constants . 更确切地说,它们也必须是编译时常量 The following example illustrates the difference: 以下示例说明了不同之处:

public class InnerStaticFinal {
    class InnerWithConstant {
        static final int n = 0;
        // OKAY! Compile-time constant!
    }
    class InnerWithNotConstant {
        static final Integer n = 0;
        // DOESN'T COMPILE! Not a constant!
    }
}

The reason why compile-time constants are allowed in this context is obvious: they are inlined at compile time. 在此上下文中允许编译时常量的原因显而易见:它们在编译时内联。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我们不能在(非静态)内部类(Java 16 之前)中使用静态方法? - Why can't we have static method in a (non-static) inner class (pre-Java 16)? 为什么匿名类在 Java 中不能有静态成员(最终成员除外)? - Why can't an anonymous class have static members (other than final ones) in Java? 为什么我在Java中的同一个类中没有抽象和静态方法 - Why can I not have an abstract and static method in the same class in Java 我们可以在Java的非静态类中具有静态块吗? - can we have static block in non static class in java? 在匿名类中使用最终字段,在方法内声明静态嵌套类,并在内部类内定义静态成员 - Using Final Fields in Anonymous Classes, Declaring Static Nested Class Inside a Method and Defining Static Members inside an Inner Class 当我们有 currentThread() 方法时,为什么 Thread 类有静态方法? - Why does Thread class has static methods when we have currentThread() method? 为什么我们必须将方法声明为java中递归调用的静态方法? - Why do we have to declare method as static for recursive call in java? 我们可以在java中的一个类中有多个静态锁吗 - Can we have multiple static locks in a class in java 是静态内部类的成员,默认情况下是java中的静态 - are the members of a static inner class by default static in java 我们无法从静态方法访问非静态实例,但可以启动类。 怎么样? - We cant access non static instance from a static method, but can initiate a class. how?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM