Learn Java Access Modifiers

Sharing is healthy !!

Hey guys !! Today we will learn java access modifiers.

In this post we will see what are access modifiers and how we can use access modifiers in java. Basically you have seen the use of public in my previous posts, like defining class or some method in a class using public or private or protected. These are called access modifiers and what access modifier mean is it basically allow you to access method or class in a package depending upon the access modifier. We can access it inside the class or inside our package. So how we can grant the access and what keyword means we will see now,

Learn Java Access Modifiers: types of access modifiers

Learn Java Access Modifiers

Basically we have three kinds of access modifier. One is called public, other is called protected and third one is called private. If you have not given any access modifier to variable then that variable will be visible inside the package and inside the class.

public access modifier

Let’s understand public access modifier with an example,

Also Read – Array Copy Program In Java

public class MyEmployee {
 
public String name;
public int id;

}

In the above example i have given public before my name and id variable. So it’s visible everywhere in your class, in your package and in your subclass(subclasses will be covered when we will learn inheritance). Basically if you define a variable as public its visible everywhere. It’s a good practice you know not to give access to your variables outside your package or class. So the best practice is always to declare your member variable as private or protected. The basic logic behind this is you don’t want that variable should be affected by any other user.

private access modifier

Let’s understand private access modifier with an example,

public class MyEmployee {
 
private String name;
private int id;

}

As you can see in the above example whenever you add private access modifier before variable name it’s only accessible through class itself. Outside the class, that is, outside MyEmployee class you cannot access if this member variable is private.

Now we cannot access String name of MyEmployee class directly, that is, see below

Learn Java Access Modifiers

So how we can allow user to set name of the employee?? It’s easy!! By using setter and getter function, like this,

public class MyEmployee {
 
public String name;
public int id;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

}

So now we have created four methods using “Generate getters and setters” in java. First one is getName() which just returns value of name. So indirectly we are accessing the value of name but not directly. setName() allows to set the name but not directly but using the copy. In the same way we have defined the getter and setter for variable age.

And now to set the name, instead of directly setting the name you can just write like this,

em.setName("Steve");

To get the name you can do something like this,

System.out.println(em.getName());
Sharing is healthy !!