在Java中使用内部类名和对象名相同

时间:2022-06-02 22:39:17

In the following code snippet, presumably it appears that it should issue some compilation error but it doesn't:

在下面的代码片段中,可能会出现一些编译错误,但没有:

class Outer {
    public static class Inner {
        static String obj = "Inner";
    }

    static Optional Inner = new Optional();
    //The (inner) class name and the object name are same.
}

class Optional {
    String obj = "Optional";
}

public class Main {

    public static void main(String[] args) {
        System.out.println(Outer.Inner.obj);
        //Refers to the string inside the optional class
    }
}

The class Outer has a static class inside it named Inner. Additionally, it declares an object (static) of the class Optional (static Optional Inner = new Optional();)

类外层有一个名为Inner的静态类。此外,它声明类Optional的对象(静态)(静态可选的内部= new Optional();)

This object and the class names (inside the class Outer) are same which is Inner. The program displays Optional. The only expression Outer.Inner.obj within main() is expected to display Inner but it doesn't. The actual output is however Optional which is the case of the Optional class.

这个对象和类名(在类外部)是相同的,这是内部的。程序显示可选的。唯一Outer.Inner表达式。在main()中,obj预计会显示内部,但它不会。但是,实际的输出是可选的,这是可选类的情况。

One way to display Inner is by changing the object name to something else.

显示内部的一种方法是将对象名更改为其他内容。

static Optional Inner1 = new Optional();

From the output it displays, it appears that the object name (or a variable) is chosen over a type name (the class Inner) because they have the same name. What exact case is applied here?

从它所显示的输出中可以看出,对象名称(或变量)是在类型名称(类内部)上选择的,因为它们具有相同的名称。这里用什么具体的例子?

3 个解决方案

#1


13  

Paragraph 6.4.2 of the Java Language Specification has some information about the rules that apply in this case.

Java语言规范的第6.4.2段有一些关于在本例中应用的规则的信息。

A simple name may occur in contexts where it may potentially be interpreted as the name of a variable, a type, or a package. In these situations, the rules of §6.5 specify that a variable will be chosen in preference to a type, and that a type will be chosen in preference to a package. Thus, it is may sometimes be impossible to refer to a visible type or package declaration via its simple name. We say that such a declaration is obscured.

在可能被解释为变量、类型或包的名称的上下文中,可能会出现一个简单的名称。在这些情况下,§6.5的规则指定一个变量将在偏好类型,选择这一种将选择优先方案。因此,有时可能不可能通过其简单的名称引用可见的类型或包声明。我们说这样的宣言是模糊的。

This refers to paragraph 6.5 Determining the Meaning of a Name, which explains the rules in detail.

这是指第6.5段确定名称的含义,其中详细解释了规则。

In your example, Outer.Inner could refer to the type of the nested class named Inner, or the static member variable Inner. The rules say the variable will be chosen over the type.

在你的例子中,外。Inner可以指名为Inner的嵌套类的类型,或者静态成员变量Inner。规则规定变量将在类型中进行选择。

#2


3  

Actually the class name is Outer$Inner.

实际上,类名是外部$Inner。

Inner classes are essentially a hack introduced in Java 1.1. The JVM doesn't actually have any concept of an inner class, and so the compiler has to bodge it. The compiler generates class B "outside" of class A, but in the same package, and then adds synthetic accessors/constructors to it to allow A to get access to it.

内部类本质上是Java 1.1中引入的一种技巧。JVM实际上没有任何内部类的概念,因此编译器必须对其进行修改。编译器在类A的外部生成类B,但在同一个包中,然后添加合成访问器/构造函数,以允许A访问它。

Checkout the following post:

检查以下职位:

Java inner class visibility puzzle

Java内部类可见性难题

#3


2  

Think of the inner class has actually have its own .java file. That will make it clear to you why it chooses the variable over the Inner class.

内部类实际上有自己的.java文件。这将使您清楚为什么它选择变量而不是内部类。

#1


13  

Paragraph 6.4.2 of the Java Language Specification has some information about the rules that apply in this case.

Java语言规范的第6.4.2段有一些关于在本例中应用的规则的信息。

A simple name may occur in contexts where it may potentially be interpreted as the name of a variable, a type, or a package. In these situations, the rules of §6.5 specify that a variable will be chosen in preference to a type, and that a type will be chosen in preference to a package. Thus, it is may sometimes be impossible to refer to a visible type or package declaration via its simple name. We say that such a declaration is obscured.

在可能被解释为变量、类型或包的名称的上下文中,可能会出现一个简单的名称。在这些情况下,§6.5的规则指定一个变量将在偏好类型,选择这一种将选择优先方案。因此,有时可能不可能通过其简单的名称引用可见的类型或包声明。我们说这样的宣言是模糊的。

This refers to paragraph 6.5 Determining the Meaning of a Name, which explains the rules in detail.

这是指第6.5段确定名称的含义,其中详细解释了规则。

In your example, Outer.Inner could refer to the type of the nested class named Inner, or the static member variable Inner. The rules say the variable will be chosen over the type.

在你的例子中,外。Inner可以指名为Inner的嵌套类的类型,或者静态成员变量Inner。规则规定变量将在类型中进行选择。

#2


3  

Actually the class name is Outer$Inner.

实际上,类名是外部$Inner。

Inner classes are essentially a hack introduced in Java 1.1. The JVM doesn't actually have any concept of an inner class, and so the compiler has to bodge it. The compiler generates class B "outside" of class A, but in the same package, and then adds synthetic accessors/constructors to it to allow A to get access to it.

内部类本质上是Java 1.1中引入的一种技巧。JVM实际上没有任何内部类的概念,因此编译器必须对其进行修改。编译器在类A的外部生成类B,但在同一个包中,然后添加合成访问器/构造函数,以允许A访问它。

Checkout the following post:

检查以下职位:

Java inner class visibility puzzle

Java内部类可见性难题

#3


2  

Think of the inner class has actually have its own .java file. That will make it clear to you why it chooses the variable over the Inner class.

内部类实际上有自己的.java文件。这将使您清楚为什么它选择变量而不是内部类。