Home > Java > Javagetting Started > body text

Can a java source file only have one public class?

王林
Release: 2020-07-08 15:01:32
Original
4854 people have browsed it

A java source file can only have one public class. A Java source file can have multiple classes, but there can only be one public class, and the public class name must be consistent with the file name. There can be only non-public classes in a file. If there is only one non-public class, this class can be different from the file name.

Can a java source file only have one public class?

#A java source file can only have one public class.

(Recommended learning: java introductory program)

Analysis:

Whether one ".java" source file can include multiple class (not inner class)? What are the restrictions?

A java source file can have multiple classes, but there can only be one public class, and the public class name must be consistent with the file name. There can be only non-public classes in a file. If there is only one non-public class, this class can be different from the file name.

(Video tutorial recommendation: java video tutorial)

Why can there be only one public class in a java source file?

1. Each compilation unit (file) can only have one public class, which means that each compilation unit has a single public interface, which is represented by a public class. This interface can contain as many classes as required to support package access. If there is more than one public class in a compilation unit, the compiler will give an error message.

2. The name of the public class must be exactly the same as the name of the file containing the compilation unit, including upper and lower case. If they don't match, you'll also get a compilation error.

3. Although it is not very commonly used, it is possible to have no public classes in the compilation unit at all. In this case, you can name the file whatever you want.

The above is the detailed content of Can a java source file only have one public class?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template