In Java, the public keyword is an access modifier for class, method and variable:

    •          When a class is marked as public, it can be accessed from anywhere, including outside packages.
    •           When a method is marked as public, it can be invoked not only from the enclosing class, but also from outside classes.
    •           When a variable is marked as public, it can be accessed and updated from outside classes.

To summary, public is the access modifier that has least restriction on the object it modifies.

The following code example shows a public class which has a public method eat() and a public variable name:

public class Person {
    public String name;
    public void eat() {
    }
}

 

Related keyword: protected and private. See all keywords in Java.

 

Related Topics:

 

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment