Learn java access modifiers

Hey guys!! Welcome to flower brackets blog. Today we will learn java access modifiers.

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.

Also read – system array copy

These are called access modifiers and what access modifier mean is, it 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,

Types of access modifiers

Learn Java Access Modifiers

Basically we have three kinds of access modifier public, protected and  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,

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

In the above example I have given public before “name” and “id”. So it’s visible everywhere in your class, in your package and in your subclass (subclasses will be covered when we will learn inheritance).

If you define a variable as public its visible everywhere. It’s a good practice not to give access to 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 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 class, that is, outside class MyEmployee you cannot access as this member variable is private.

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

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;
   }
}

Now we have created four methods using “Generate getters and setters” in java eclipse.

First one is getName() which 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 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());

Related Posts