Public Access Applied to a Class

When the public access modifier is applied to a standard outer class, the following access is granted:

  • Full access is granted to inherit the class from within the same package or from any other package.
  • Full access is granted to create a new reference to an instance from within the same package or from any other package.
  • A source code file (.java) can contain a maximum of one public class and the name of the source code file must be the name of the public class. Other default access classes can exists in the same file.

It is very important to understand that applying public access to a class declaration does not set or change the access of the members and elements enclosed inside of the class.

Things to think about

  • If you do not explicitly create a constructor for the class, a default constructor will be implicitly created with public access.
  • Be careful of constructors that you create – a more restrictive access applied to a constructor can cause compiler errors.


Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md one
C:\Java>cd one
C:\Java\one>Notepad IAmPublic.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


package one;

import two.YouArePublic;

//class Tester extends IAmPublic { // Inheritance in same package
//class Tester extends YouArePublic { // Inheritance in different package
class Tester {

    public static void main(String args[]) {
        // Reference in same package
        // IAmPublic iap = new IAmPublic();
        // iap.displayMessage();

        // Inheritance in same package or different package
        // Tester ot = new Tester();
        // ot.displayMessage();

        // Reference in different package
        // YouArePublic yap = new YouArePublic();
        // yap.displayMessage();

    }
}

public class IAmPublic {

    public void displayMessage() {
        System.out.println("I am public in package ONE!");
    }    
}

Now switch back to the command prompt (CMD) and type in the following commands.


C:\Java\one>cd ..
C:\Java>md two
C:\Java>cd two
C:\Java\two>notepad YouArePublic.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


package two;

public class YouArePublic {
    String message = "You are public in package TWO!";

    // there is a default public no-argument constructor right here
    // YouArePublic() { super(); }

    // YouArePublic(String message) {
    //    super();
    //    this.message = message;
    //}

    public void displayMessage() {
        System.out.println(message);
    }    
}

Now switch back to the command prompt (CMD) and type in the following commands.


C:\Java\one>cd ..
C:\Java>javac one\IAmPublic.java
C:\Java>javac two\YouArePublic.java
C:\Java>java one.Tester // play around with uncommenting and comment various statements.
Varying results


Final thoughts

It is very important to understand that applying public access to a class declaration does not set or change the access of the members and elements enclosed inside of the class.


Tutorials