Public vs Package Access Modifiers in Java
Last Updated :
18 Apr, 2022
Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by using an appropriate keyword in java called access modifiers. So access modifiers are used to set accessibility of classes, methods, and other members.
Lets us discuss both of the modifiers as follows:
- Public Access Modifiers
- Package(Default) Access Modifier
Modifier 1: Public Access Modifiers
If a class is declared as public then we can access that class from anywhere.
We will be creating a package pack1 inside that package we declare a class A which is public and inside that class, we declare a method m1 which is also public. Now we create another package pack2 and inside that pack2 we import pack1 and declare a class B and in class B’s main method we create an object of type class A and trying to access the data of method m1.
Example 1:
Java
// Java Program to illustrate Public Access Modifiers
// creating a package
package pack1;
// import required packages
import java.io.*;
import java.util.*;
// declaring a public class
public class A {
// declaring method m1
public void m1() { System.out.println("GFG"); }
}
Output: Compiling and saving the above code by using the below command line:

Here we will be importing the above class of the created package to the newly created package.
Example 2:
Java
// Java Program to illustrate Public Access Modifiers
// creating a package
package pack2;
// importing class from above package
// package pack1
// import required packages
import java.io.*;
import java.util.*;
import pack1.A;
// Main class
class B {
// Main driver method
public static void main(String[] args)
{
// Creating an object of type class A
A a = new A();
// accessing the method m1()
a.m1();
}
}

Output Explanation:
If class A is not public while compiling B class we will get a compile-time error saying pack1. A is not public in pack1 and can’t be accessed from the outside package. Similarly, a member or method, or interface is declared as public as we can access that member from anywhere.
Modifier 2: Package(Default) Access Modifier
A class or method or variable declare without any access modifier then is considered that it has a package(default)access modifier The default modifier act as public within the same package and acts as private outside the package. If a class is declared as default then we can access that class only within the current package i.e from the outside package we can’t access it. Hence, the default access modifier is also known as the package–level access modifier. A similar rule also applies for variables and methods in java.
Example:
Java
// Java Program to illustrate Package Level Access Modifier
// Importing utility classes
// Importing input output classes
import java.io.*;
import java.util.*;
// Main Class
class GFG {
// Declaring default variables that is
// having no access modifier
String s = "Geeksfor";
String s1 = "Geeks";
// Method 1
// To declare a default method
String fullName()
{
// Concatenation of strings
return s + s1;
}
// Method 2
// Main driver method
public static void main(String[] args)
{
// Creating an object of main class(GFG)
// in the main() method
GFG g = new GFG();
// Calling method1 using class instance
// and printing the concatenaion of strings
System.out.println(g.fullName());
}
}
Output:
GeeksforGeeks
Finally, after understanding and going through both of them let us conclude the evident differences between them which are depicted in the table below as follows:
Public Access Modifier | Package Access Modifier |
---|
Public members can be accessed from a non-child class of outside package. | This modifier can't be accessed from a non-child class of the outside package. |
Public members can be accessed from child class of outside package. | We cannot access this modifier from the child class of the outside package. |
The public modifier is more accessible than the package access modifier. | This modifier is more restricted than the public access modifier. |
Similar Reads
Public vs Private Access Modifiers in Java
Whenever we are writing our classes we have to provide some information about our classes to the JVM like whether this class can be accessible from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not etc. we can specify this information by usi
3 min read
Package vs Private Access Modifiers in Java
Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by usi
3 min read
Public vs Protected Access Modifier in Java
Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by usi
4 min read
Protected vs Package Access Modifiers in Java
Whenever we are writing our classes, we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not, etc. we can specify this information by usi
4 min read
Abstract vs Public Access Modifier in Java
Access Modifier in Java is the reserved keyword used to define the scope of a class, variable, and methods. It also tells us about that whether child class creation is possible or not or whether object creation is possible or not. Abstract Access Modifier is a modifier applicable only for classes an
4 min read
Protected vs Private Access Modifiers in Java
Access modifiers are those elements in code that determine the scope for that variable. As we know there are three access modifiers available namely public, protected, and private. Let us see the differences between Protected and Private access modifiers. Access Modifier 1: Protected The methods or
2 min read
Public vs Protected vs Package vs Private Access Modifier in Java
Whenever we are writing our classes we have to provide some information about our classes to the JVM like whether this class can be accessible from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not etc. we can specify this information by usi
7 min read
Java - Final vs Static Access Modifier
The final keyword is used in different contexts. First, final is a non-access modifier applicable only to a variable, a method, or a class. The following are different contexts where the final is used. The static keyword in Java is mainly used for memory management. The static keyword in Java is use
5 min read
Access Modifiers in Java
In Java, access modifiers are essential tools that define how the members of a class, like variables, methods, and even the class itself can be accessed from other parts of our program. They are an important part of building secure and modular code when designing large applications. Understanding de
7 min read
Private vs Final Access Modifier in Java
Whenever we are writing our classes we have to provide some information about our classes to the JVM like whether this class can be accessed from anywhere or not, whether child class creation is possible or not, whether object creation is possible or not etc. we can specify this information by using
3 min read