What is Access And Non-Access Modifiers In Java? - Study24x7
Social learning Network
study24x7

Default error msg

Login

New to Study24x7 ? Join Now
Already have an account? Login

What is Access And Non-Access Modifiers In Java?

Updated on 15 February 2020
study24x7
Java Object oriented programmi
7 min read 11 views
Updated on 15 February 2020

Types of modifiers in java

  1. Access Modifier
  2. Non-Access Modifier 


The visibility of a class, method, constructor, and variable is controlled by access modifiers. Whereas non-access modifiers are used to provide other functionalities like synchronizing a method or block, restricting the serialization of a variable, etc. 


Access Modifier

Types of access modifier in java-

  1. Private
  2. Default
  3. Protected
  4. Public 


Private Access Modifier- Private access modifiers are basically used before method, constructor, and variable. Private access modifiers can not be used with class and interface.


It can be used with the class when the class in the inner class i.e. class inside another class. Private is the most restricted level of access.


Important Points About access Modifier-

  1. The class in which constructor is declared as private, the object can not be created for that object.
  2. The variable which is declared as private can be accessed using the getter method only. 


Example- 


package Java_Test; 

//class with private variable class PrivateModifier { 

private int a = 10; 


public int getA() { 

return a; 

} public void setA(int a) { this.a = a; } } public class AccessModifiers { 


public static void main(String[] args) { 

PrivateModifier pm = new PrivateModifier(); System.out.println(pm.getA()); 


}


Default Access Modifier

Default access modifiers are basically used before method, constructor, and variable(If we are not declaring any modifier then it is considered as default access modifier). If we declare any method, constructor or variable as default then it will be accessed from any class within the package. 


Protected Access Modifier

Protected access modifiers are basically used before method, constructor, and variable. Protected can not be used with the class. If we declare any method, constructor or variable as protected then it will be accessed from any class within the package and outside the package through child class. 


Public Access Modifier

A public access modifier is accessible anywhere. 


Non Access Modifier-

There are various non-access modifiers in java. They are used to provide some information to JVM.

  • Final
  • Static
  • Synchronized
  • Abstract 
  • Transient 


Final

  • It can be used with class, variable, and method. 
  • The class which is declared as final can not be inherited. 
  • The method which is declared as final can not be overridden. 
  • The variable which is declared as final can not be changed once assigned. 


Static


  • It can be used with block, variable and method and nested class. 
  • When a variable is declared as static then it is initialized when class is loaded. 
  • The static variable gets memory only once at the time of class loading. 
  • The method which is declared as static can be called directly with the class name. synchronized- 
  • The synchronized keyword is used to control the access of multiple threads to any shared resource. 


Abstract


  • The class has at least one unimplemented method(not mandatory) and declared as abstract is known as abstract class. 
  • An abstract class is used to achieve abstraction.


Transient


  • Java transient keyword is used in serialization. The data member declared as transient will not be serialized. 



Know More:


What is Garbage Collection ?

Follow our page JAVA Object oriented programming Language, where you can find every topic related to java


study24x7
Write a comment...
Related Posts