Why is there only one public class in a Java source file?

Source: Internet
Author: User

Statement, this article is reproduced

Reproduced

http://blog.csdn.net/bareheadzzq/article/details/6562211

Recently implemented in a Java file a few classes, one is declared public type, but the compiler error, and then the public removed, that is, the file does not have a public class, the program runs normally, some confusion, finally through this article to find the answer, for later convenient search, reprint, thank the author.

Conclusion:

There can be at most one public class in a Java source file, and when there is a public class, the source file name must be

Must be consistent or not compile, if there is no public class in the source file, there is no consistency requirement in the file name and class.
The main () does not have to be placed in the public class to run the program.

Reprint content:

Idle all right, see a post on the internet asked why a Java source file can only have one public class?
Someone on the net answered: http://topic.csdn.net/t/20060528/22/4784755.html
, each compilation unit (file) can have only one public class. What this means is that each
A unit can have only one open interface, and this interface is represented by its public class.

I think this is a conclusion from software architecture design and security design. Or the designers of Java are considering this. Maybe it's really a spec, but I didn't find the information.

I don't know if I have this conversation. If you have a known colleague to give a source of information?

The experiment is as follows:
Test3.java source file:
Class Test1
{
int i = 1;


}

Class Test2
{
int i = 2;
public static void Main (string[] args)
{
System.out.println ("Main method");
}
}

C:/javatest>javac Test3.java

C:/javatest>java Test2
Main method

Note: Compile without error, note is running Test2 because there is no Test3.class file generated. Error if running TEST3

The class could not be found
C:/javatest>java Test3
Exception in thread "main" java.lang.NoClassDefFoundError:Test3
caused By:java.lang.ClassNotFoundException:Test3
At Java.net.urlclassloader$1.run (Unknown Source)
At java.security.AccessController.doPrivileged (Native Method)
At Java.net.URLClassLoader.findClass (Unknown Source)
At Java.lang.ClassLoader.loadClass (Unknown Source)
At Sun.misc.launcher$appclassloader.loadclass (Unknown Source)
At Java.lang.ClassLoader.loadClass (Unknown Source)
Could not find the main class:test3. Program would exit.
The reason for this error is simple: the class loader in the JVM cannot find the Test3.class

It also shows that the class containing main () is not necessarily public if it wants to run.

In the second edition of in-depth JVM, there is this sentence:
The Java Virtual machine instance runs a Java program by invoking the main () of a class, and the main () must be public
static void and receives an array of strings as arguments, any class that has such a main () can be used as a Java thread

The starting point of the sequence.
It is not said that the class owning the main () method must be a public class.


Test7.java source file:
Class Test5
{
int i = 1;


}

public class Test6
{
int i = 2;
public static void Main (string[] args)
{
System.out.println ("Main method");
}
}

If you run Test7.java error:
C:/javatest>javac Test7.java
Test7.java:8: Class Test6 is public and should be declared in a file named Test6.java
public class Test6
^1 Error
This shows that the file name must match the class name of the public class (if there is a public class in the file)

Here you can see if there are more than one public class, then the file name should be which public class? Obviously a Java source file

There can be only one public class.

So the summary is as follows: There can be at most one public class in a Java source file, and when there is a public class, the source file name must be

Must be consistent or not compile, if there is no public class in the source file, there is no consistency requirement in the file name and class.
The main () does not have to be placed in the public class to run the program.

The above is the conclusion through the experiment, the individual thinks to here already can, if must ask the end, may want to question the Java

The designer of the platform. Perhaps, people will say: This is the design of Java and the internal architecture of the JVM caused by the design, this is a rule

Fan, there is no reason why.

Why is there only one public class in a Java source file?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.