Java Modifier

Java has two type of modifier, Access control modifier and Non-Access Modifier.

Access control modifier

There are four access modifiers available in Java, used to set access levels for classes, variable methods and constructor.
  • Scope only inside the same package (default)
  • Scope is visible to world (public)
  • Scope within the package and all sub classes (protected)
  • Scope only within the classes only (private)

Non-Access modifier

There are five non-access modifiers available in Java, used to achieve many other functionality.
  • Modifier to finalizing the implementations of classes, methods, and variables (final)
Example:
class Phone
{
 final int PRICE_MIN = 999;
 final int PRICE_MAX = 5600; //final variable
 
 final void display() //final method
 {
  System.out.println("Min Price is" + PRICE_MIN);
  System.out.println("Max Price is" + PRICE_MAX );
 }
}
  • Modifier to create class methods and variables (static)
Example:
class Programming {
  public static void main(String[] args) {
    display();
  }
 
  static void display() {
    System.out.println("I love to programming in Java.");
  }
}

  • Modifier for creating abstract classes and methods (abstract)
  • Modifiers for use in threads (synchronized and volatile)



                                                                      ⏪ PREVIOUS PAGE          NEXT PAGE ⏩

1 comment: