Tutorials Hut

Access Modifiers In Java

Access modifiers in java are reserved keywords in java programming languages which are used with classes, variables, methods and constructors to define their scope, for example a class with public key word(modifier) means it is available to be used in the entire java project.

We will see more examples and types of access modifiers in java in the article below.

Types of Access Modifiers in Java

There are two types of modifiers in java:

  • Java Access Modifiers
  • Non Access Modifiers

Java Access Modifiers

Java access modifiers are used to define access level or scope of any variable, class or method. We can use any of the below before naming our variables, classes or method to limit their scope or accessibility.

  • Default modifiers – These do not need any keyword, if you don’t specify a specific modifier variable or class will fall under this which means visible to package level.
    Any code within the package may access it.
  • Private modifiers – Only visible to the class in which it is used, this is very restrictive and should be used if scoped is really within a class, for example a variable or a method which is needed in the same class to support another method or code, we may make them private.

Keyword used: private

  • Public modifiers – Public modifiers are visible to the entire project or JVM, it is used a lot because most of the time we want all our methods and classes accessible across project or JVM.
    Keyword used: public
  • Protected modifiers – Protected modifiers are visible within the package to all sub classes and classes.

Keyword used: protected

Example:

// class with public access modifier
public class Garden{
 
   void printDetailsOfGarden(int floweNumber) {
   	System.out.println("I am a garden class, total flowers are:"+floweNumber);
   }
 
	//Method with private modifier
	private int getNoOfFlowers() {
   	return 30;
	}
 
	public static void main(String args[]) {
    	Garden garden = new Garden();
    	garden.printDetailsOfGarden(garden.getNoOfFlowers());
	}
 
	// A inner class Plants with private modifier
	private class Plants{
 
	}
}

Non-Access Modifiers

There are some key words which are widely used in java for different purposes, we may call them non-access modifiers, we may see more details in coming chapters.

  • Static modifier – static keyword is with variables, methods in class to make them statically accessible which means we can directly access them with  <class name>.<name of variable/method>
  • Final modifier – final keyword is used with class, variables and methods which ensures once they are defined nothing in code changes them, for example a int variable declared with final keyword will stop any change in its value later from anywhere in the entire project.
  • Abstract modifier – abstract this keyword is used to create abstract classes, we will see more details about abstract in the coming chapter, it helps in achieving OOPs concept abstraction.
  • Synchronized and volatile – synchronized and volatile keywords are used in java multi threading, we will see more details about this topic in coming chapters.

Example:

// class with public access modifier
public class Garden{
 //final and static in used, please not such declarations will have variable names in caps letters
	final static int GARDEN_AGE = 30 ;
 
   void printDetailsOfGarden() {
   	System.out.println("I am a garden, I am :"+GARDEN_AGE+" years old.");
   }
 
	//main method with static key word
	public static void main(String args[]) {
    	Garden garden = new Garden();
    	garden.printDetailsOfGarden();
	}
 
	// A inner class Plants with abstract modifier
	abstract class Plants{
 
   	//an abstract method
   	abstract void plantDetails();
 
	}
}
You might have noticed, we have used the same classes to show different examples and scenarios, this will enable you to understand the concepts without worrying about thousands of classes, with good fundamentals it will be very easy to develop complex codes in java.

References

Access modifiers by oracle : Access Modifiers

Next Article To Read


















  • 1 thought on “Access Modifiers In Java”

    Leave a Reply

    Your email address will not be published. Required fields are marked *