public (Java)

public is an access modifier that makes a class, method, or field visible and usable from any other class in the program. It is the least restrictive level of access.

Java

Access modifiers control who can use a piece of code. public means 'available everywhere,' while private restricts access to within the same class. There is also protected and a default (package-private) level.

The main method must be public so the JVM can call it from outside your class.

Examples

Java

public class Account {
    public int balance = 100;
}
// balance can be read from any other class:
// Account a = new Account();
// System.out.println(a.balance);  -> 100

Learn it in a lesson

See also

Back to the Dictionary