Java Keyword Public

The reserved keyword public is used in coordination with the declaration of a class, variable, or method. A public classes, methods, and variables can be accessed by the members of any class. The public access modifier makes it visible to all.

Get more details on Java access modifiers and the proper way to use them.

The sample code below defines a public class that has a public variable as well as a public method.

//JavaCodeDepot Sample 

public class Widget {

    public String widgetName;

    public void processWidget() {

         //Some widget actions go here...
    }
}


The public access modifier is the least restrictive modifier in terms of access from outside classes.

The complete list of Java reserved keywords contains a total of 50 keywords and 3 literal words that are unable to be used as variable names.

Add a Comment

Your email address will not be published. Required fields are marked *