Access Modifiers in Java


Java provides several access modifiers to set the accessibility of classes, constructors, variables, and methods. The most common ones are public, protected, default (no modifier), and private.

Source Code

public class Person {
    private String name; // Private access modifier: Accessible within the same class only
    public String getName() { // Public access modifier: Accessible from any other class
        return name;
    }
}

Choose the most restrictive access level that makes sense for your member variables and methods to encapsulate and protect your data.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments