In Java, access modifiers decide who can access a class, field, constructor, or method. Among the four access levels, public is the most accessible. When a member is declared public, it can generally be accessed from other classes and packages, provided the referenced class itself is accessible.
Think of a public member as something displayed at the entrance of a building. Anyone who is allowed to enter the building can use it. This makes public useful for functionality that a class intentionally exposes to the outside world.
Why Does the public Modifier Exist?
Classes often need to communicate with other parts of an application. For example, a payment service may provide a method such as processPayment() that other components need to call. Making that method public creates an explicit entry point into the service.
public means the member is accessible from anywhere the containing class is accessible.
However, public does not mean that everything inside a class should be exposed. Good object-oriented design exposes only the operations that other classes genuinely need.
Basic Syntax of public
public returnType methodName() { // method body }
The public keyword is placed before the member declaration. It can be used with classes, methods, fields, and constructors, depending on the declaration context.
public Method Example
class Calculator { public int add(int a, int b) { return a + b; } } public class Main { public static void main(String[] args) { Calculator calculator = new Calculator(); int result = calculator.add(10, 20); System.out.println(result); } }
Here, the add() method is public. The Main class can therefore call it through a Calculator object.
A public method is often used as part of the external interface of a class. It tells other classes, “This operation is available for you to use.”
public Field Example
A field can also be declared public.
class Student { public String name; } public class Main { public static void main(String[] args) { Student student = new Student(); student.name = "Rahul"; System.out.println(student.name); } }
The field name is directly accessible from the Main class because it is public.
Although this is legal Java, public fields are usually discouraged in well-designed applications because any accessible code can modify the value directly. This can make validation and data control difficult.
public Class Example
A top-level class can also be declared public.
public class Employee { public void displayDetails() { System.out.println("Employee details"); } }
A public top-level class can be accessed from other packages when the package is imported or the class is referenced using its fully qualified name.
public Constructor Example
Constructors can also be public. A public constructor allows code outside the class to create objects using the new keyword.
class Product { public Product() { System.out.println("Product created"); } } public class Main { public static void main(String[] args) { Product product = new Product(); } }
Because the constructor is public, Main can create a Product object. If the constructor were private, external code could not create the object directly in the same way.
public Across Packages
One of the most important reasons to understand public is its behaviour across packages. Suppose a class belongs to one package and another class belongs to a different package. A public member can be accessed across that package boundary when the containing class is accessible.
package store; public class ProductService { public void showProduct() { System.out.println("Product information"); } }
Another class in a different package can use ProductService after importing it.
package app; import store.ProductService; public class Main { public static void main(String[] args) { ProductService service = new ProductService(); service.showProduct(); } }
The public access level makes the ProductService class and its showProduct() method available outside the store package.
public Does Not Mean “Everything Is Safe”
A common beginner mistake is to assume that public is automatically the best choice because it provides maximum accessibility. In professional Java development, the opposite approach is often better: expose only what consumers need.
For example, imagine a BankAccount class with a balance field. Making the balance public would allow external code to assign invalid values directly.
class BankAccount { public double balance; }
Another class could then perform an uncontrolled modification:
BankAccount account = new BankAccount();
account.balance = -50000;
This is one reason Java developers commonly keep data private and expose controlled public methods instead.
Use public to expose intentional functionality, not simply to make every member accessible.
public Method vs public Field
| Feature | public Method | public Field |
|---|---|---|
| Accessibility | Accessible from allowed external code | Accessible from allowed external code |
| Control | Can control operations through logic | Direct value access |
| Validation | Can validate input before changing state | Difficult to control direct changes |
| Typical Usage | Common for class APIs | Usually avoided for mutable data |
Common Beginner Mistakes
- Making every field public simply because other classes need access to it.
- Assuming public automatically means the class is available regardless of the containing class or package structure.
- Using public members without considering encapsulation.
- Confusing public access with unrestricted modification of an object's internal state.
- Exposing implementation details that should remain internal to the class.
Best Practices for Using public
- Use public for operations that form the intended external interface of a class.
- Prefer public methods over public mutable fields.
- Keep implementation details private whenever possible.
- Design the public API carefully because other code may depend on it.
- Avoid exposing more functionality than consumers actually need.
Interview Insight
In an interview, remember that public provides the widest access level among Java's four access modifiers. A public member can be accessed from classes in the same package and from classes in other packages, subject to the accessibility of the containing type and normal Java rules.
A strong interview answer should go one step further: public access is not just about visibility. It is also an API design decision. Once functionality becomes public, other parts of an application may rely on it, so unnecessary public members can increase coupling and make future changes harder.
Quick Revision
| Concept | Key Point |
|---|---|
| public | Provides the widest normal access level in Java. |
| public method | Allows external code to invoke the method when the containing type is accessible. |
| public field | Allows direct external access to the field. |
| public constructor | Allows external code to create objects using that constructor. |
| Cross-package access | Public members can be accessed across package boundaries when the containing type is accessible. |
| Design principle | Expose necessary functionality publicly and keep implementation details hidden. |
The public modifier is the doorway through which other parts of a Java application interact with a class. It is powerful because it provides broad visibility, but good design is not about making everything visible. The real skill is deciding what deserves to be part of the public interface and what should remain safely hidden inside the class.
