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.
#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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.