Public Access Applied to a Constructor

When the public access modifier is applied to a constructor, the following access is granted:

  • Full access is granted to invoke the constructor from within the same package or from any other package.

Things to think about

  • Multiple overloaded constructors can have different types of access modifiers applied to them. In other words, if you mark one constructor as public, other constructors can be default, protected, or private access.
  • Be mindful of the class access type. A standard outer class can only have either public or default access. A constructor with the same access type as the class is the norm. Constructors with a more restrictive access may cause compiler errors if the class is inherited or initialized as an object.
  • Public classes with public constructors can expose elements and members from other more restrictive classes to outside package access.


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 ConstructorOne.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.*;

class Tester {

    public static void main(String args[]) {
        new ConstructorOne(); // from package one
        new ConstructorTwo(); // from package two
    }
}

public class ConstructorOne {

    public ConstructorOne() { 
        super(); 
        System.out.println("Public constructor 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 ConstructorTwo.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 ConstructorTwo {

    public ConstructorTwo() { 
        System.out.println("Public constructor in package TWO!");
    }
      
}

class IAmSafeInPackageTwo {
    IAmSafeInPackageTwo() {
        System.out.println(">>>Package two - default access class with default access constructor  ... ");
        System.out.println("with default access method was just accessed in package one");
        methodInPackageTwo();        
    }

    void methodInPackageTwo() {
        System.out.println("Method in package two exposed outside of package <<<");
    }
}

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


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


Final thoughts

One must learn and understand certain rules in Java. When those rules are not known, overlooked, or ignored, there can be profound effects. For example, the first statement in a constructor must be a call to either super() or this(). If you do not explicitly code in a super(...) or this(...) statement, then the compiler will implicitly put in a default no-argument call to super(). If someone does not know that rule, but they learned how to inherit a class by using the extends keyword, then they learned just enough to become dangerous!
There is an old saying, "you need to learn to walk before you can run", and that applies to programming Java as well. Learning the fundamentals block by block is the key to avoiding frustration and major issues down the road.


Tutorials