ProgramGuru

Java Reference Library →

public Keyword Usage and Examples

public Keyword Usage and Examples

Learn about the Java public keyword: how it's used in classes, methods, and variables. Understand scope, accessibility, and visibility with practical examples.

This reference explains public Keyword Usage and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

public Keyword in Java

In Java, the public keyword is an access modifier. It's used to define the visibility of classes, methods, constructors, and variables across different packages. When something is marked as public, it means that it can be accessed from anywhere — within the same class, same package, subclass, or from completely unrelated classes in other packages.

Why Use public ?

Java is built around the principle of encapsulation, and access modifiers like public help control that access. You use public when you want your component to be accessible globally. It’s common for main classes, APIs, and utility methods.

1. Public Class Example

A public class must be declared in a file with the same name as the class. This is often used when defining entry-point classes for your applications.

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, world!");
    }
}
Hello, world!

Explanation: The class HelloWorld is declared public, so it can be accessed from anywhere. The main method is also public so the JVM can call it.

2. Public Method

Marking a method public makes it callable from any other class, even in other packages.

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}
public class Demo {
    public static void main(String[] args) {
        Calculator calc = new Calculator();
        System.out.println(calc.add(5, 3));
    }
}
8

Explanation: The add method is public, so it can be accessed from the Demo class even if they are in separate packages (provided proper import).

3. Public Variable

Although not recommended (due to encapsulation best practices), variables can also be marked public if you want direct access.

public class Dog {
    public String breed = "Labrador";
}
public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        System.out.println(dog.breed);
    }
}
Labrador

Explanation: Here, the breed variable is directly accessible. However, in practice, it’s better to use private variables with public getters/setters.

4. Public Constructor

Constructors can also be made public so that objects can be created freely.

public class Book {
    public Book() {
        System.out.println("Book created!");
    }
}
public class Library {
    public static void main(String[] args) {
        Book b = new Book();
    }
}
Book created!

Explanation: The constructor is public, so any other class can create an instance of Book.

When Not to Use public

  • When you want to limit access to within a package — use default.
  • To encapsulate internal logic — use private.
  • For inheritance-restricted access — use protected.

Best Practices

  • Only expose what is necessary — avoid making everything public.
  • Favor encapsulation: use getters/setters instead of public fields.
  • Document public methods and classes thoroughly — they are your API surface.

Summary

Usage Accessible From
public class Everywhere
public method All classes, packages
public variable Globally (not recommended)
public constructor Anyone can instantiate

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →