A Java file can only have one public class

Keywords: Java

There are three sentences in the access rights of class 6.4 in Java programming ideas 4th Edition:

  1. Each compilation unit (file) can only have one public class. This means that each compilation unit has a single public interface, which is implemented by the public class. The interface can contain many classes that support package access rights as required. 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 exactly match the file name containing the compilation unit, including case. Therefore, for a Widget, the name of the file must be Widget.java, not Widget.java or Widget.java.
  3. Although it is not very commonly used, it is possible to compile a unit without a public class at all. In this case, you can name the file at will.

Let's first understand "package access rights" in the first sentence.
As we know, public, protected and private are access modifiers in Java. What if you don't provide permission modifiers and explicitly specify that a member is public, protected or private? At this time, it refers to "package access permission" (sometimes "package access permission" is expressed as friendly). "Package access permission" means that all other classes in the current package can access this member, but all classes outside the package do not have access to this member.
For example: create a new Java project, create a new Package: com.example.tester under the src directory of the project, and create a new class Hello under the Package. Among them, the class Person with Package access permission is defined in the class Hello file.

package com.example.tester;

class Person{
    private String name;
    private int age;
    public Person(String name,int age){
        this.name = name;
        this.age = age;
    }
    public void display(){
        System.out.println(name+"is "+age);
    }
}

public class Hello {
    public static void main(String[] args) {
        Person p = new Person("Nicholas",18);
        p.display();
    }
}

To facilitate observation and comparison, create a new class Test under the com.example.tester package, and define a class Check outside the com.example.tester package, as shown below:

package com.example.tester;
public class Test {
    public static void main(String[] args) {
        Person p = new Person("Jackson",24);
        p.display();
    }
}
import com.example.tester.Person;

public class Check {
    public static void main(String[] args) {
        Person p = new Person("Tomas",28);
        p.display();
    }
}

It can be seen that the classes Hello and Test in the package can access person, while the class Check outside com.example.tester does not have access to person. The error message is: 'com.example.tester.Person' is not public in 'com. Example. Tester'. Cannot be accessed from outside package.

Therefore, the first sentence means that there can be at most one public class in a Java file. If there are multiple classes, all classes except public are friendly classes, that is, these classes do not have access permission to modify words in front of them.
The second sentence means that if there is a public class in a Java file, the Java file name must be consistent with the public class name.
The third sentence means that a Java file can be all friendly classes. At this time, the Java file name can be arbitrary. Let's take an example.
Create a new Java file HelloWorld under the com.example.tester package. In this file, two classes are defined: Hello and World. These two classes are friendly classes, as follows:

package com.example.tester;

class Hello{
    public static void main(String[] args) {
        System.out.println("hello");
    }
}

class World{
    public static void main(String[] args) {
        System.out.println("world");
    }
}

Reference articles

Why can there be only one public class in a java source file
Java programming ideas 4th Edition

Posted by arsitek on Tue, 12 Oct 2021 12:28:17 -0700