Java Modifiers

 Modifiers are Java keywords that are used to declare features in applications. They affect either the lifetime or the accessibility of a feature. A feature may be a class, a method, or a variable.

Modifiers that affect the visibility of a feature are called access modifiers or visibility modifiers. The remaining modifiers do not fall into any clear categorization and may be called storage and lifetime modifiers.

Java modifiers are of two types :

  • Access Modifier 
  • Non-Access Modifier


Access Control Modifiers

Java provides a number of access modifiers to set access levels for classes, variables, methods and constructors. 

The four access levels are:

  • Default
  • Public
  • Private
  • Protected
Default: Visible to the package only. No modifiers are needed. 
In sample code below, you can see that no access modifier is used with 'class' keyword which makes it default. Now this class is only visible/accessible to the other classes which are present in the same package along with this class. Same goes for default variables and methods.

class MyClass {

  public static void main(String[] args) {
    System.out.println("Java World");
  }

}

Public: Visible to the world
In sample code, 'public' is used before 'class' keyword which makes this class as public. It means this class is visible/accessible to all the other classes of the project.

public class Main {

  public static void main(String[] args) {
    System.
out.println("Hello World");
  }

}

Private: Visible to the class only
In sample code, 'private' keyword is used with different variables. It means these variables can be used/accessed only within this class. 

Important Tip: One very important point to note here is that, a top-level class can't be private. But we can declare inner-class as private. If we declare a class as private, java compiler will complain that 'private' is not allowed here.

public class Main {

  private String fname = "John";
  private String lname = "Deer";
  private String email = "john@deer.com";
  private int age = 24;

  public static void main(String[] args) {
    Main myObj = new Main();
    System.out.println("Name: " + myObj.fname + " " + myObj.lname);

    System.out.println("Email: " + myObj.email);
    System.out.println("Age: " + myObj.age);
  }
}


Protected: Visible to the package and all subclasses
In sample code, 'protected' is used with some variables which means these variables can only be accessed/visible in subclass/child class of 'Person' class. Same applies to methods also. Inheritance comes into picture here.

Important Tip: Class cannot be made protected. A class can either be default or public.

class Person {

  protected String fname = "John";
  protected String lname = "Doe";
  protected String email = "john@doe.com";
  protected int age = 24;

}

class Student extends Person {

  private int graduationYear = 2018;

  public static void main(String[] args) {
    Student myObj = new Student();
    System.out.println("Name: " + myObj.fname + " " + myObj.lname);

    System.out.println("Email: " + myObj.email);

    System.out.println("Age: " + myObj.age);
    System.out.println("Graduation Year: " + myObj.graduationYear);

  }
}


Non-Access Modifiers

Java provides a number of non-access modifiers to achieve many other functionality.

·      The static modifier for creating class methods and variables.

·      The final modifier for finalizing the implementations of classes, methods, and variables.

·      The abstract modifier for creating abstract classes and methods.

·      The synchronized and volatile modifiers, which are used for threads.



Java Data Types   << Previous     ||     Next >>  Java Operators



Follow Us

Quora Space | Facebook Page | Telegram Channel
Feel free to ask queries or share your thoughts in comments or email us.

Comments

Popular posts from this blog

10 Best Demo Websites for Automation Testing Practice

Automate 'User Registration' of E-commerce Website using Selenium

Automate E-Commerce Website using Selenium Webdriver

Automate Browser Actions on Godaddy.com using Selenium