Skip to content

Modifiers Used In Java Programming Language

In this article, you are going to learn different types of Modifiers used in Java Programming Language.

Modifiers are the special keywords or simply specifiers that are appended to the beginning of class, variables, methods and constructors in order to control their accessibility(visibility) and other functionalities.

Modifiers also help to achieve a certain degree of encapsulation, an object oriented programming concept, in Java language. If You Are a beginner and need consulting, you may join our Java training in Gurgaon to enhance your skills in Java development.

Now as you have learned the basic definition of the term modifier, it’s time to make you familiar with different kinds of modifiers used in Java Language. These are described below in detail.

TYPES OF MODIFIERS IN JAVA PROGRAMMING LANGUAGE

In Java, modifiers are classified into two main groups:

  • Access Modifiers
  • Non-access Modifiers

[A] ACCESS MODIFIERS

The modifiers or keywords which are used to achieve certain extent of accessibility or visibility in a java program are known as access modifiers. These modifiers are well known for defining the object oriented concept ‘Encapsulation’.

Access Modifiers in Java are divided into following four sub-parts:

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

(1) Default Access Modifier:

When no access modifiers are attached to the class, variables, methods and constructors, they by default are accessible to any other class. All the fields and methods remain public by default.

Example:class W3TrainingSchool {
String student;
W3TrainingSchool  (String studentName) {
student = studentName;
}
void printStudentName() {
System.out.println(“Student name is :” + student);
}
public static void main(String args[]) {
W3TrainingSchool  trial = new W3TrainingSchool (“Sachin”);
trial.printStudentName();
}
}
Output:Student’s name is : Sachin

(2) Public Access Modifier:

Public access modifier is used to make the class, variables, methods, constructors etc. accessible to other class.

The fields and methods declared inside a public class are always accessible to any other class in a java program. Thus, having a greater scope as compared to other access modifiers.

Example:public class W3TrainingSchool {
public static String trainer;
public W3TrainingSchool  (String trainerName) {
trainer = trainerName;
}
public void printTrainerName() {
System.out.println(“Trainer’s name is :” + trainer);
}
public static void main(String args[]) {
W3TrainingSchool  trial = new W3TrainingSchool (“Amit Negi”);
trial.printTrainerName();
}
}
Output:Trainer’s name is : Amit Negi

(3) Private Access Modifier:

Private access modifier is used to make the variables, methods, etc. accessible only within the class. If these variables and methods are tried to get accessed outside the class, a compile time will be generated.

However, if public methods are present in the class, the private declared variables can be accessed outside the class.

Private access modifier is not applicable to a class and interface.

Encapsulation can be achieved through private access modifier that helps to make the data hidden from the outside sphere and ultimately provides high security.

Example 1: In this example, private access modifier is not used, therefore accessing the variables and methods outside the class will result in the successful execution of the program.class W3TrainingSchool {
int students = 300;
void numberOfStudents() {
System.out.println(“This is the number of students in W3Training School“);
}
}class AboutW3TS {
public static void main(String args[]) {
W3TrainingSchool obj=new W3TrainingSchool();
System.out.println(obj.students );
obj.numberOfStudents();
}
}
Output 1:3oo
This is the number of students in W3Training School
Example 2: In this example, we have attached private access modifier to the variable and the method, now accessing them outside the class will result in a compilation error.class W3TrainingSchool {
private int students = 300;
private void numberOfStudents() {
System.out.println(“This is the number of students in W3Training School“);
}
}class AboutW3TS {
public static void main(String args[]) {
W3TrainingSchool obj=new W3TrainingSchool();
System.out.println(obj.students );
obj.numberOfStudents();
}
}
Output 2:Main.java:17: error: students has private access in class W3TrainingSchool
System.out.println(obj.students );Main.java:18: error: numberOfStudents() has private access in class W3TrainingSchool
obj.numberOfStudents();2 errors

(4) Protected Access Modifier:

Protected access modifier is used to make variables, methods etc. accessible by the class within the package.

They cannot be accessed by the class(except its subclass) outside the package.

These modifiers are only applicable to the fields and methods. They have a restriction on class and interfaces.

Example:class W3TrainingSchool {
protected boolean username1 = true;
protected boolean username2 = false;
protected void numberOfStudents() {
System.out.println(“Username 1 is :” + username1);
System.out.println(“Username 2 is :” + username2);
}
}class W3TS {
public static void main(String args[]) {
W3TrainingSchool obj=new W3TrainingSchool();
obj.numberOfStudents();
}
}
Output:Username 1 is : true
Username 2 is : false

[B] NON-ACCESS MODIFIERS

There are number of other modifiers used in java programming language which do not deal with the accessibility of a class, variable, method, constructor etc. but affects the functionality of a java program. These are known as Non-access modifiers.

Some of the non-access modifiers are listed below.

  1. Static Modifier
  2. Final Modifier
  3. Abstract Modifier
  4. Synchronized Modifier
  5. Transient Modifier
  6. Volatile Modifier
  7. Native Modifier
  8. Strictfp Modifier

(1) Static Modifier:

Static modifier is used to create variables and methods. It is not applicable to a class and local variables of the class.

When variables and methods are declared with a static modifier, they become independent of any other instances of the class.

Example:
public static string name;
protected static int numberOfStudents() {

(2) Final Modifier:

Final Modifier can be used with class, variables(including local variable), and methods. If a class, variable or method is declared with a final modifier, it means they are no longer available for reference.

No further implementations can be done except manipulation of the data within in the object.

Example:
public static final int age = 25;

(3) Abstract Modifier:

Abstract modifiers are used with class( to make it extensible) and methods. It should be noted that whenever an abstract method is declared in a class, the class should also be declared with an abstract modifier. Ignoring this will result in a compilation error.

Abstract modifiers cannot be used with ‘final’ for both class and method. It should also be noted that an abstract method should always contain a semi-colon at the end.

Example:
public abstract class W3TrainingSchool {
public abstract void numberOfStudents();

(4) Synchronized Modifier:

This modifier is applicable to methods. Declaring a method with a synchronized modifier means a single action can be performed at a time. It can be used along with default, public, private and protected modifiers.

Example:
private synchronized void getAge() {

(5) Transient Modifier:

A transient modifier is usually attached to an instance variable in order to make it unavailable while serializing the object. They are not applicable to local variables.

Example:
public transient int age = 25;

(6) Volatile Modifier:

Volatile modifier is used to make a variable editable by other threads after being allowed by the compiler.

Example:
private volatile boolean enrolled;

(7) Native Modifier:

Native modifiers are attached to the methods and are used to access resources and call libraries only accessible by other languages. The native methods end with a semicolon.

Example:
public native void printName();

(8) Strictfp Modifier:

This modifier is used in classes, methods, and interfaces. If a class is declared with a strictfp modifier, it means the code inside the class will follow or act in accordance with the floating point IEEE 754 arithmetic standard rules.

Strictfp modifier is not with variables, abstract methods, and constructors.

Example:
strictfp class W3TrainingSchool {
strictfp void userName() {

Also Check : Types of Variables in Java Programming Language


We have provided you the description on different types of modifiers used in java programming language. Hope you like this article. For more updates and related information, stay connected to our blogs on java programming language.

Facebook
Twitter
LinkedIn
Pinterest