Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is really “No Public Class” reachable within its package? [duplicate]

Tags:

java

In "Think in Java", the author says:

You just leave the "public" keyword off the class, in which case it has package access. (That class can be used only within that package.)

To prove this, I create one public class and one no-public class:

package com.ciaoshen.thinkinjava.chapter7;
import java.util.*;

//My public class
public class PublicClass {
    //default constructor
    public PublicClass(){
        System.out.println("Hello, I am PublicClass.");
    }
}

//Non public class
//It should be package reachable
class PackageReachableClass {
    //default constructor
    PackageReachableClass(){
        System.out.println("Hi, I am PackageReachableClass.");
    }
}

But when I call them from another class in the same package:

public class InPackageClass {

/**
 *  MAIN
 *  @param args void
 */
public static void main(String[] args){
    //pubic class can be reached from anywhere
    PublicClass newPublicClass=new PublicClass();
    //non-public-class should be accessable in the same package
    PackageReachableClass newPackageReachableClass =new PackageReachableClass();
}
}

The system warning me: The no-public class should not be accessed from outside its own source file.

/Users/Wei/java/com/ciaoshen/thinkinjava/chapter7/InPackageClass.java:22: warning: auxiliary class PackageReachableClass in ./com/ciaoshen/thinkinjava/chapter7/PublicClass.java should not be accessed from outside its own source file
        PackageReachableClass newPackageReachableClass =new PackageReachableClass();
        ^
/Users/Wei/java/com/ciaoshen/thinkinjava/chapter7/InPackageClass.java:22: warning: auxiliary class PackageReachableClass in ./com/ciaoshen/thinkinjava/chapter7/PublicClass.java should not be accessed from outside its own source file
        PackageReachableClass newPackageReachableClass =new PackageReachableClass();
                                                            ^
2 warnings
Hello, I am PublicClass.
Hi, I am PackageReachableClass.

So here comes my question: Is the no-public class still package reachable? And why Java forbid us to call them from another file in the same package if this is totally legal?

like image 985
shen Avatar asked Mar 19 '16 18:03

shen


People also ask

Why can't we have more than one public class in the same file?

If you try to create two public classes in same file the compiler generates a compile time error.

Why can't we have two public classes in Java?

No, while defining multiple classes in a single Java file you need to make sure that only one class among them is public. If you have more than one public classes a single file a compile-time error will be generated.

How many public classes are there per source code file?

Each source file should contain only one public class and the name of that public class should be similar to the name of the source file. If you are declaring a main method in your source file then main should lie in that public class.

How many public classes can be there in a Java program?

A single Java program can contain more than one class and there are no restrictions on the number of classes that can be present in one Java program.


1 Answers

It is only a warning that you have defined an auxiliary class.

auxiliary class PackageReachableClass in PublicClass.java

Which means you have one Java file with 2 classes, which is against Java Code Conventions. (Emphasis mine).

Each Java source file contains a single public class or interface. When private classes and interfaces are associated with a public class, you can put them in the same source file as the public class. The public class should be the first class or interface in the file.

To correctly test your package-private classes, please separate the two classes into separate files based on their class name, i.e. PublicClass.java and PackageReachableClass.java.

like image 54
OneCricketeer Avatar answered Sep 21 '22 02:09

OneCricketeer