Modifier in java definition

  • Modifier are keyword that we add to the defination to change it's meanings.
  • Java has two variety of Modifier-
    1. Access Modifier
    2. Non-Access Modifier

Flow Diagram of if statement

Access Modifier

Access Modifier has four  modifier to control access levels for classes, variable methods and constructor-

Default

  • Default access modifier means we do not explicitly declare an access modifier for a class, field, method, etc.
  • Default has scope only inside the same package.
  • A variable or method declared without any access control modifier is available to any other class in the same package.

Public

  • Public has scope that is visible everywhere
  • A class, method, constructor, interface, etc. declared public can be accessed from any other class.
  • If the public class we are trying to access is in a different package, then the public class still needs to be imported.

Protected

  • Protected has scope within the package and all sub classes.
  • We cannot applied protected access modifier to class and interfaces.

Private

  • Private has scope only within the classes.
  • A class, method, constructor, interface, etc. declared public can be accessed from any other class.

Non-Access Modifier

Final

  • When a variable is declared with final keyword, its value can’t be modified, essentially, a constant. 
  • When a method is declared as final, then that method cannot be overridden.

Static

  • The static modifier is associated only with methods and variables, not classes. 
  • The static modifier is used to specify a method that can only be declared once.
  • Static methods are used to specify methods that should not be overridden in subclasses.

Transient

  • When an instance variable is declared as transient, then its value doesn't persist when an object is serialized

Synchronized

  • When a method is Synchronized it can be accessed by only one thread at a time. We will discuss it in detail in Thread.

Volatile

  • Volatile modifier tells the compiler that the volatile variable can be changed unexpectedly by other parts of your program.
  • Volatile variables are used in case of multithreading program. 
  • Volatile keyword cannot be used with a method or a class. It can be only used with a variable.