DEV Community

Cover image for Access Modifiers in Java: The Ultimate Gatekeepers
Krunal Kanojiya
Krunal Kanojiya

Posted on • Originally published at techalgospotlight.com

Access Modifiers in Java: The Ultimate Gatekeepers

Hey there, I am back with another exciting java topic! Are you ready to take your coding skills to the next level? Today, we’re going to explore one of the most fundamental concepts in Java access modifiers!

What's an Access Modifier, Anyway?

Imagine you're at a exclusive club, and you need to get past the bouncer to get in. The bouncer checks your ID, and if you're on the list, you're in! Access modifiers are like the bouncers of Java – they control who can access your classes, methods, and variables.

Types of Access Modifiers in Java

Java has four types of access modifiers: public, private, protected, and default (also known as "package-private"). Each modifier has its own set of rules, so let's dive in and explore each one!

1. public Access Modifier

The public access modifier is like the VIP pass of Java – anyone can access your classes, methods, and variables from anywhere.

public class MyClass {
    public void myMethod() {
        System.out.println("Hello, World!");
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the MyClass class and its myMethod() method are accessible from anywhere.

2. private Access Modifier

The private access modifier is like the secret password of Java – only the class itself can access its own private members.

public class MyClass {
    private int myVariable;

    public void myMethod() {
        myVariable = 10;
        System.out.println(myVariable);
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the myVariable variable is private, and only the MyClass class itself can access it.

3. protected Access Modifier

The protected access modifier is like the family secret of Java – only the class itself and its subclasses can access its protected members.

public class MyClass {
    protected int myVariable;

    public void myMethod() {
        myVariable = 10;
        System.out.println(myVariable);
    }
}

public class MySubClass extends MyClass {
    public void mySubMethod() {
        myVariable = 20;
        System.out.println(myVariable);
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the myVariable variable is protected, and both the MyClass class and its subclass MySubClass can access it.

4. default (or "package-private") Access Modifier

The default access modifier is like the neighborhood secret of Java – only classes within the same package can access its default members.

// MyClass.java
package com.example.mypackage;

public class MyClass {
    int myVariable;

    public void myMethod() {
        myVariable = 10;
        System.out.println(myVariable);
    }
}

// MyOtherClass.java
package com.example.mypackage;

public class MyOtherClass {
    public void myOtherMethod() {
        MyClass myClass = new MyClass();
        myClass.myMethod();
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, the myVariable variable is default (or package-private), and both the MyClass class and the MyOtherClass class within the same package can access it.

Conclusion

And there you have it, folks! Access modifiers are the gatekeepers of Java, controlling who can access your classes, methods, and variables. By using the right access modifier, you can ensure that your code is secure, maintainable, and scalable.

So, go ahead and start using access modifiers in your Java code. Your code (and your data) will thank you!

Top comments (2)

Collapse
 
joodi profile image
Joodi

Oh Java, my faithful companion! Java was the first language I started programming with. 🙌

Collapse
 
imkrunalkanojiya profile image
Krunal Kanojiya

Yeahhh. Soon i will share complex topics with simple examples.