Java Modifiers

Java Modifiers are keywords that have a special function. There are two basic types of Java modifiers.

  • Java Access Modifiers
  • Non Access Modifiers

Java Access Modifiers

Java Access Modifiers are the keywords that define the type of access of variable, method or class. They are written with the definition of class, method or variable. The access depends on the type of Java Modifier used in the definition. The table below shows the list of Access Modifiers and their definitions in Java programming language.

Sr.No.ModifierDescription
1.publicAccessible from everywhere and every class.
2.privateAccessible only within its own class.
3.protectedAccessible by the same package and sub classes
4.defaultWhen no access specifier is mentioned, it is set to default. Default provides access only within the same package.
Java Modifiers (Access)

Non Access Modifiers

Other than access modifiers there are also some non access modifiers defined by Java. These modifiers have different functionalities. A list of non access java modifiers is described in the table below:

Sr.No.ModifierDescription
1.finalThis modifier means that the implementation of class, methods and attributes is final and cannot be changed. It does not allow overriding.
2.staticThis modifier creates class specific methods and attributes rather than object specific.
3.abstractThis modifier creates abstract classes and methods. It is used in an abstract class to make an abstract method.
4.synchronizedThis modifier allows to access the method by one thread at a time.
5.volatileThis modifier allows to read the value of method or attribute from main memory rather than from cache.
6.transientThis modifier allows to skip methods and attributes when serializing their object.
Java Modifiers (Non Access)

Example

The below code uses various java modifiers.

public class className {
   // piece of code
}

private boolean check;
static final double weight = 9.5;
protected static final int quantity = 42;

public static void main(String[] arguments) {
   // piece of code
}

In the above code, the check variable is declared private so it cannot be accessed from outside the class. Child class or sub class cannot access this variable. However, the quantity variable can be accessed by child and sub classes as it is declared protected. the quantity variable can be access the quantity variable can be access the quantity variable

final

If as a programmer, you do not want to change the value of your java variable, you can create a final variable using final keyword. Its value once assigned cannot be changed or altered later in the program.

For example, the below code will give an error on the console because it tries to change the value of final variable:

public class MyProgram {
  public static void main(String[] args) {
    final int var = 20;
    var = 40;                  // will raise an error on console
    System.out.println(var);
  }
}

The error displayed on console will be something like this: error can not ass sign a value to a fin car car car etc. to 40 err

abstract

An abstract method can be declared inside an abstract class. It has no body. It has no body It has no body

The code below shows the concept of abstract methods and abstract class. It has no body It has no body It has no body

// abstract class in file number 1
abstract class Main {
  public String name = "John";
  public int age = 25;
  public abstract void post(); // abstract method 
}

// Subclass 
class Employee extends Main {
  public int workingSince = 2018;
  public void post() {
    System.out.println("Software Engineer");
  }
}
// second class in file number 2
class Second {
  public static void main(String[] args) {
    
    Employee myObj = new Employee(); 
    
    System.out.println("Name: " + myObj.name);
    System.out.println("Age: " + myObj.age);
    System.out.println("Working Since: " + myObj.workingSince);
    myObj.post(); // call abstract method
  }
}

The output of the above code after compilation is: name john age 25 working since 2018 post soft engg

Comments are closed.