Public vs private Java

Hanno Van Der Walt
3 min readDec 13, 2022

--

In this article, we will be discussing the differences between the public and private access modifiers that Java provides us with, as well as get some hands-on experience.

Access modifiers, also known as specifiers, are used to specify the restrictions of a member, such as a class or function. Java gives us three access modifiers to make use of, namely public, private, and protected. We do not have to use these modifiers and if we choose not to, the member will have default access.

Public

Starting with public, and to make it a bit easier to understand let’s think of access modifiers as swimming pools with different access. With public swimming pools, anyone from anywhere can make use of it. Same goes for the public access modifier and we can access the variables, classes, and methods from anywhere in the program when we declare it as public.

Let’s create a class called Pool and define a public function called swim, which prints out some swimming related words:

public class Pool {

public void swim(){
System.out.println("Splish Splash in public swimming pool!");
}

}

Next, create the LetsGoSwim class and implement the main method in which we create a Pool object named publicPool and call the swim function by executing the main method:

public class LetsGoSwim {

public static void main(String args[]){

Pool publicPool = new Pool();
publicPool.swim();

}

}

The following is the output, and we see the program has no problem accessing the class and its functions:

Splish Splash in public swimming pool!

Private

Think of the private modifier as a private swimming pool or a pool in your property. Only the people who are on your property and within the walls can swim in it. Same goes for private variables and functions, it can only be accessed or called from within the class where it has been declared and not from anywhere in the program like with public.

Let’s use the same classes as before, but now let’s make the swim function private :

public class Pool{

private void swim(){
System.out.println("Splish Splash in private swimming pool!")
}

}

When we run the main method, we get the following error. It tells us that we can’t access the swim function in the Pool class because it has private access, and we are trying to call it outside of the class where it has been declared.

java: swim() has private access in Pool

So, you might be asking how we use these private methods ? Let’s say there is a way to swim in this private pool by asking the owner of the house if you can swim in his pool. He says you can, only if you wear a swimming cap, so your hair doesn't clog the pumps.

Let’s make a method called askOwnerToSwim and give it an access modifier of public. Because anyone from anywhere must be able to ask the owner if they can swim in his pool. Let’s also pass the function a Boolean variable isWearingSwimmingCap to indicate whether you’re wearing a swimming cap or not. Inside the function, we check the isWearingSwimmingCap variable, and if it is true, it calls the private swim function.

public class Pool {

private void swim(){
System.out.println("Splish Splash in private swimming pool with my swimming cap!");
}

public void askOwnerToSwim(Boolean isWearingSwimmingCap){

if(isWearingSwimmingCap){
swim();
}

}

}

In the main class, we make a Pool object called private pool, and called the askOwnerToSwim method and passed it a Boolean value of true.

public class LetsGoSwim {

public static void main(String args[]){

Pool privatePool= new Pool();
privatePool.askOwnerToSwim(true);

}

}

When running the above program, we get the following output :

Splish Splash in private swimming pool with my swimming cap!

Which now works, because we call the private swim function from within the class where it has been declared.

--

--

Hanno Van Der Walt

I am a passionate young software engineer with a love for learning, aviation and the outdoors.