Questions about the public class in the Java source file

Source: Internet
Author: User

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 the same as otherwise it cannot be compiled;

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 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 Test3.class, and the 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 calling Main () of a class, and this main () must be public static void and receive an array of strings as a parameter, and any class that has such a main () can be the starting point for the Java program.
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 can have only one public class.

Through the above experiments, we can draw the conclusion of the beginning!

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

Questions about the public class in the Java source file

Related Article

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.