在java中创建与类同名的对象

时间:2022-09-25 10:14:41

In C++ when I create an object like the following, then no more objects can be created for the same class.

在c++中,当我创建如下这样的对象时,就不能为同一个类创建更多的对象。

Box Box; //Box is the class Name

Here Box becomes an object and whenever we use Box again the compiler recognizes it as an object. But in the case of java this isn't.

这里的Box变成了一个对象,每当我们再次使用Box时,编译器都会将它识别为一个对象。但在java的例子中,情况并非如此。

Box Box = new Box(); 
Box box = new Box(); //valid 

What is the reason behind this?

这背后的原因是什么?

3 个解决方案

#1


7  

Basically, Java has slightly different set of syntax rules, by the sounds of it. When the grammar says you've got a variable declaration with an initializer, such as this:

基本上,Java的语法规则集稍有不同,这是由它的声音决定的。当语法说您有一个带有初始化器的变量声明时,例如:

Box box = new Box();

... it knows that Box has to be the name of a type, not the name of a variable. So it doesn't matter whether or not there's a variable called Box in scope. (That applies to the new operator as well.)

…它知道框必须是类型的名称,而不是变量的名称。所以在作用域中是否有一个叫做Box的变量并不重要。(这也适用于新运营商。)

I don't know the intimate details of the C++ syntax, but it sounds like it's not set up to make that distinction, at least in the example you've given. It's not like it's a feature as such - it's just a matter of how names are looked up by the compiler.

我不知道c++语法的具体细节,但听起来它并不是用来区分的,至少在您给出的示例中是这样的。这并不是说它本身就是一个特性——它只是一个如何由编译器查找名称的问题。

#2


5  

Java is using different namespaces for different types of identifiers. The restricted syntax of Java makes it unambiguous whether you are referring to a type, a function, a variable or a label.

Java对不同类型的标识符使用不同的名称空间。Java受限制的语法使您可以明确地引用类型、函数、变量或标签。

#3


2  

For details about how a name lookup works in C++ see section 3.4 in the standard.

有关c++中名称查找如何工作的详细信息,请参阅标准中的3.4节。

In the case you described the basic idea is this: for an unqualified name (like Box your code), the compiler starts searching for a declaration in the current scope before moving up. When it finds a declaration for that name, any declaration, it stops. So in your case, when you try to declare a new variable of type Box, it finds the declaration of the variable 'Box' and stops searching. It just assumes that 'Box' is a variable name and doesn't look any further.

在您描述的情况下,基本思想是这样的:对于不合格的名称(如框入代码),编译器在向上移动之前开始在当前范围内搜索声明。当它找到该名称的声明时,任何声明都会停止。所以在你的例子中,当你尝试声明一个类型为Box的新变量时,它会找到变量'Box'的声明并停止搜索。它只是假设“Box”是一个变量名,不会再看得更远。

#1


7  

Basically, Java has slightly different set of syntax rules, by the sounds of it. When the grammar says you've got a variable declaration with an initializer, such as this:

基本上,Java的语法规则集稍有不同,这是由它的声音决定的。当语法说您有一个带有初始化器的变量声明时,例如:

Box box = new Box();

... it knows that Box has to be the name of a type, not the name of a variable. So it doesn't matter whether or not there's a variable called Box in scope. (That applies to the new operator as well.)

…它知道框必须是类型的名称,而不是变量的名称。所以在作用域中是否有一个叫做Box的变量并不重要。(这也适用于新运营商。)

I don't know the intimate details of the C++ syntax, but it sounds like it's not set up to make that distinction, at least in the example you've given. It's not like it's a feature as such - it's just a matter of how names are looked up by the compiler.

我不知道c++语法的具体细节,但听起来它并不是用来区分的,至少在您给出的示例中是这样的。这并不是说它本身就是一个特性——它只是一个如何由编译器查找名称的问题。

#2


5  

Java is using different namespaces for different types of identifiers. The restricted syntax of Java makes it unambiguous whether you are referring to a type, a function, a variable or a label.

Java对不同类型的标识符使用不同的名称空间。Java受限制的语法使您可以明确地引用类型、函数、变量或标签。

#3


2  

For details about how a name lookup works in C++ see section 3.4 in the standard.

有关c++中名称查找如何工作的详细信息,请参阅标准中的3.4节。

In the case you described the basic idea is this: for an unqualified name (like Box your code), the compiler starts searching for a declaration in the current scope before moving up. When it finds a declaration for that name, any declaration, it stops. So in your case, when you try to declare a new variable of type Box, it finds the declaration of the variable 'Box' and stops searching. It just assumes that 'Box' is a variable name and doesn't look any further.

在您描述的情况下,基本思想是这样的:对于不合格的名称(如框入代码),编译器在向上移动之前开始在当前范围内搜索声明。当它找到该名称的声明时,任何声明都会停止。所以在你的例子中,当你尝试声明一个类型为Box的新变量时,它会找到变量'Box'的声明并停止搜索。它只是假设“Box”是一个变量名,不会再看得更远。