search
HomeJavaJavagetting StartedIs there a public class required in a java source file?

Yes, there can be only one public class in a source file. Because each compilation unit (file) can only have one public class, that is, each compilation unit has a single public interface, represented by a public class; this interface can contain numerous classes that support package access permissions as required.

Is there a public class required in a java source file?

The operating environment of this tutorial: windows10 system, java8, Dell G3 computer.

Question: Can a ".java" source file include multiple classes (not inner classes)? What are the restrictions?

Answer: There can be 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.

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

There are three paragraphs like this in the book Java Programming Thoughts (Fourth Edition) (6.4 Class Access Permissions):

1. Each compilation unit (file) has There can only be one public class, which means that each compilation unit has a single public interface, 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 common, 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.

What happens when there is no public class in the java source file?

 

At this time, the program is compiled and three .class files are generated under the corresponding path. If you click Run directly, the program will not run. We need to right-click the program----->Run As----->Java Aplication----->Select the data source

Personal summary:

A compilation unit (java file) can have multiple classes, and multiple different .classes are generated during compilation. Files and .class files are the source of data for program operation. Java uses public classes as the data interface of each compilation unit. There can only be one, otherwise it cannot handle java files with multiple classes. When a compilation unit (java file) has multiple non-public classes, the data source needs to be selected at runtime.

Recommended related video tutorials: Java video tutorial

The above is the detailed content of Is there a public class required in a java source file?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment