The public keyword in Java is an access modifier used for fields, methods, and classes. Members declared as public are accessible from any other class. This is the most permissive access level, allowing unrestricted access to the member or class from any other class in any package.
Table of Contents
- Introduction
publicKeyword Syntax- Understanding
public - Examples
- Public Fields
- Public Methods
- Public Classes
- Real-World Use Case
- Conclusion
Introduction
The public keyword in Java is used to declare fields, methods, and classes that should be accessible from any other class. It provides the highest level of access control, ensuring that the member or class can be used freely across different packages and classes.
public Keyword Syntax
Public Field:
public dataType fieldName; Public Method:
public returnType methodName(parameters) { // method body } Public Class:
public class ClassName { // class body } Output:
Understanding public
Key Points:
- Access Level: Public members and classes can be accessed from any other class in any package.
- Visibility: Public members and classes are visible and accessible throughout the entire application.
- Use Case: Used when you want to allow unrestricted access to certain parts of your code.
Examples
Public Fields
Public fields can be accessed directly from any other class.
Example
public class Car { public String model; // Public field public Car(String model) { this.model = model; } } public class Main { public static void main(String[] args) { Car myCar = new Car("Toyota"); System.out.println("Car model: " + myCar.model); // Accessing public field } } Output:
Compilation failed. Public Methods
Public methods can be called from any other class.
Example
public class Car { private String model; public Car(String model) { this.model = model; } public String getModel() { // Public method return model; } } public class Main { public static void main(String[] args) { Car myCar = new Car("Honda"); System.out.println("Car model: " + myCar.getModel()); // Calling public method } } Output:
Compilation failed. Public Classes
Public classes can be accessed and instantiated from any other class.
Example
public class Car { private String model; public Car(String model) { this.model = model; } public void displayModel() { System.out.println("Car model: " + model); } } public class Main { public static void main(String[] args) { Car myCar = new Car("Tesla"); myCar.displayModel(); // Accessing public class } } Output:
Compilation failed. Real-World Use Case
Creating a Library
In real-world applications, the public keyword is often used to create public APIs or libraries. This allows other developers to use the functionality provided by your library without any restrictions.
Example
// File: Library.java package com.example.library; public class Library { public void displayMessage() { System.out.println("Welcome to the Library!"); } } // File: Main.java package com.example.application; import com.example.library.Library; public class Main { public static void main(String[] args) { Library library = new Library(); library.displayMessage(); // Accessing public method from public class in another package } } Output:
Compilation failed. Conclusion
The public keyword in Java is a powerful access modifier that provides the highest level of accessibility for fields, methods, and classes. By using public, you can ensure that certain parts of your code are accessible from anywhere in the application, making it suitable for creating public APIs and libraries. Understanding and using the public keyword effectively is essential for designing flexible and reusable code in Java.