英文:
In java I am trying to create an object for a class but it shows an error "The public type Add1 must be defined in its own file"
问题
我在这里遇到一个错误
错误信息:公共类型Add1必须在其自己的文件中定义
出现在类名"Add1"中
public class Testing_Main
{
public static void main(String[] args)
{
System.out.println("在创建对象之前");
Add1 obj=new Add1();
obj.add1();
System.out.println("在创建对象之后");
}
}
public class Add1
{
public int add1()
{
return 5;
}
}
英文:
Here I am getting an error
The public type Add1 must be defined in its own file
in the class name "Add1"
public class Testing_Main
{
public static void main(String[] args)
{
System.out.println("Before Object is Created");
Add1 obj=new Add1();
obj.add1();
System.out.println("After Object is Created");
}
}
public class Add1
{
public int add1()
{
return 5;
}
}
答案1
得分: 1
这种错误 公共类型 className 必须在其自己的文件中定义 发生在一个文件中有两个公共类的情况下。这是一种 Java 规则,你应该每个文件只有一个公共类。
为什么每个源文件只能有一个公共类
根据Java 语言规范(第7.6节)
当包存储在文件系统中(§7.2.1),主机系统可以选择强制执行以下限制,如果以下情况之一成立,则它是一个编译时错误:
- 该类型被其他包中的编译单元的代码引用。
- 该类型被声明为 public(因此从其他包中的代码中访问可能)。
这一限制意味着每个编译单元最多只能有一个这样的类型。这一限制使得 Java 编译器能够轻松地在包中查找命名类。实际上,许多程序员选择将每个类或接口类型放在自己的编译单元中,无论它是否是公共的或者是否被其他编译单元的代码引用。
例如,公共类型 wet.sprocket.Toad
的源代码将在 wet/sprocket
目录下的 Toad.java
文件中找到,相应的对象代码将在同一目录下的 Toad.class
文件中找到。
为了更清楚地理解,让我们假设在同一源文件中有两个公共类 public class A
和 public class B
,并且 class A
引用了尚未编译的 class B
。在编译(编译-链接-加载)类 A 时,同时链接到 class B
,编译器将被迫检查当前包中的每个 *.java
文件,因为 class B
没有自己的特定的 B.java
文件。因此,在上述情况下,编译器需要一些时间来查找哪个类位于哪个源文件中,以及主方法位于哪个类中。
因此,保持每个源文件中只有一个公共类的原因实际上是为了使编译过程更快,因为它可以在链接期间更高效地查找源文件和已编译文件。这个想法是,如果你知道一个类的名称,你就知道它应该在每个类路径条目中的哪个位置找到,而不需要索引。
英文:
This kind of error The public type className must be defined in its own file occures when we have two public classes in one file. It is a kind of java rule that you should have one public class per file.
Why Only One Public Class Per Source File
> According to Java Language Specification (Section 7.6)
>When packages are stored in a file system (§7.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:
>
> - The type is referred to by code in other compilation units of the package in which the type is declared.
> - The type is declared public (and therefore is potentially accessible from code in other packages).
>
>
>
> This restriction implies that there must be at most one such type per
> compilation unit. This restriction makes it easy for a Java compiler
> to find a named class within a package. In practice, many programmers
> choose to put each class or interface type in its own compilation
> unit, whether or not it is public or is referred to by code in other
> compilation units.
>
> For example, the source code for a public type wet.sprocket.Toad would
> be found in a file Toad.java in the directory wet/sprocket , and the
> corresponding object code would be found in the file Toad.class in the
> same directory.
To get a more clear picture, let's imagine there are two public classes public class A
and public class B
in the same source file, and class A
has reference to the not-yet-compiled class B
. And we are compiling (compiling-linking-loading) class A now while linking to class B
the compiler will be forced to check each *.java
files within the current package because class B
doesn’t have it’s specific B.java
file. So, in the above case, it is a bit time consuming for the compiler to find which class lies under which source file and in which class the main method lies.
So the reason behind keeping one public class per source file is to actually make the compilation process faster because it enables a more efficient lookup of source and compiled files during linking. The idea is if you know the name of a class, you know where it should be found for each classpath entry and no indexing will be required.
答案2
得分: 0
我们不能在一个 .java
文件中有两个公共类。您可以通过为类 Add1
创建一个单独的文件来解决这个问题。
Testing_Main.java
public class Testing_Main
{
public static void main(String[] args)
{
System.out.println("创建对象之前");
Add1 obj=new Add1();
obj.add1();
System.out.println("创建对象之后");
}
}
Add1.java
class Add1
{
public int add1()
{
return 5;
}
}
或者,如果您想将 Add1
类保留在 Testing_Main
中,则可以将 Add1
类设为非公共类。
英文:
We cannot have two public classes in one .java
file. You can solve this problem by creating a separate file for the class `Add1.
Testing_Main.java
public class Testing_Main
{
public static void main(String[] args)
{
System.out.println("Before Object is Created");
Add1 obj=new Add1();
obj.add1();
System.out.println("After Object is Created");
}
}
Add1.java
public class Add1
{
public int add1()
{
return 5;
}
}
Or If you want to keep Add1
class in Testing_Main
, then you can make the Add1
class non-public.
答案3
得分: 0
在Java中,每个类都必须与其自己的文件关联。类与文件名严格绑定,因此名为Foo
的类必须放在名为Foo.java
的文件中。因此,您必须分别将类TestingMain
和Add1
放在它们自己的文件中,即TestingMain.java
和Add1.java
。
英文:
In java, each class must be associated with its own file. The classes are strictly bound with the filename, so a class called Foo
must be places in a file called Foo.java
. Therefore, you have to place both the classes TestingMain
and Add1
in their own files, namely TestingMain.java
and Add1.java
respectively.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论