Error: Class Something is public and should be declared in a file named Something.java

Source: Internet
Author: User

Java compilation error:

  Error: Class Something is public and should be declared in a file named Something.java

As shown in the following:

The first explanation:
"Class A is public and should be declared in a file named A.java" This sentence needs to be understood in two steps :

1. In Java, if Class A is declared public, then class A must be stored in a file named A.java;
2, conversely, in a Xxx.java file contains a maximum of a top-level public class, and the public class name is the same as the file name. For example, in file A.java, one or more classes are allowed to be defined, but at most a top-level public class is allowed, and this class is named A.
  Note: the top-level emphasis here is to allow non-top-level public classes to exist, such as internal public classes (that is, public classes in inner classes).

The second explanation:
This is your naming error, change the file name to A.java because the class in the file is public class A {},
If you can get rid of this public, then the file name can be arbitrary. Otherwise the file name will be consistent with the class name.

The third explanation:
There can be at most one class in a file that is public, so you can expose the definition of a class to be accessible to other classes! If you already have this public class, the file name must be the same as the class name!

   Also note: if only one. java file is built, public can only declare before public static void main (string[] args) {}, Otherwise, even if the class name of the defined public is the same as the name of the Java file you declared, an error will be compiled.

Error: Class Something is public and should be declared in a file named Something.java

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.