Menu

Access Modifiers in Java
📰

Access Modifiers in Java

DEV Community: java·Subash·14 days ago
#2eP9aYWU
#devtojava#devto#dev#access#class
Reading 0:00
15s threshold

What are Access Modifiers?

Access modifiers are keywords in Java that define the accessibility of classes, methods, constructors and variables.

They help us control where a particular member can be accessed.

Types of Access Modifiers

Java provides four access modifiers:

public

private

protected

default

1) Public

The public modifier allows a member to be accessed from anywhere, including other packages.

public class Student {

public String name = "John" ;

public void display () { System . out . println ( name ); } }

Enter fullscreen mode

Exit fullscreen mode

Here, the name variable and display() method can be accessed from anywhere.

2) Private

The private modifier allows a member to be accessed only within the same class.

public class Student {

private int age = 22 ;

private void display () { System . out .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.