codingstreets
Search
Close this search box.

Introduction to Modifier in Java

introduction-to-modifier-in-java
Photo by Ivan Samkov on Pexels.com

Modifier in Java – This article is about Modifiers in Java, which describes about what Modifier is and illustrates various Modifiers in Java with an example.

Before moving ahead, let’s know a bit about Java Access Specifier.

Table of Contents

Modifier

A modifier in Java indicates how to use a class, method, or variable with the access level of a class.

static

A static method is used to define a class, method, or variable, which can be accessed without creating an instance of the class.

To call a static method, we don’t need to create an object, it can be called by using the class name. 

For example:

				
					public class MyClass {
    private static int count = 0;

    public MyClass() {
        count++;
    }

    public static int getCount() {
        return count;
    }
}
				
			

In the above example, the variable “count” and method getCount() are both defined as static in that “count” keeps count of the number of instances of the “MyClass” class, and method getCount() returns the value of “count

Because count is static, it is shared among all instances of the MyClass class and can be accessed without creating an instance of the class. 

For example:

				
					MyClass first = new MyClass();
MyClass second = new MyClass();
System.out.println(MyClass.getCount()); // prints 2
				
			

Here, we created two objects of the class “MyClass”, and the value of the count will keep incrementing for every new object defined. To get the value of “count”, we don’t need to create an instance of the class; it can be accessed by the method getCount().

final

A final modifier means if a class, method, or variable is defined, it cannot be modified, overridden or changed. 

For example, a final class cannot have its subclass, a final method cannot be overridden, and a variable’s value cannot be changed once assigned a value.  

Let’s look at an example of final class:

				
					public final class MyClass{
         // class members here
}
				
			

In the above example, a final class is defined named “MyClass”; it means that it cannot have a subclass.

Let’s look at an example of the final method:

				
					public class MyClass{
         public final void myMethod(){
	//method description/implementation
         } 
}

				
			

In the above example, a final method is defined named “myMethod”; it means that it cannot be overridden.

Let’s look at an example of the final variable:

				
					public class MyClass{
	public final int number = 1;
}
				
			

In the above example, a final variable is named “number”; it cannot be assigned to another value once it is set.

Conclusion: The use of final is beneficial when you want to enforce immutability and prevent accidental modification of classes, methods, or variables in your code.

abstract

An abstract modifier is used to declare an abstract class or method.

An abstract class cannot have an object; it must be subclassed.

An abstract class may contain both abstract and non-abstract methods.

An abstract method is a method that has no implementation. A subclass must override it.

Let’s look at an example:

				
					abstract class Shape {
    abstract double area();
}

class Circle extends Shape {
    double radius;

    Circle(double radius) {
        this.radius = radius;
    }

    @Override
    double area() {
        return Math.PI * radius * radius;
    }
}
				
			

In the above example, an abstract class is defined named “Shape”, and inside that class, an abstract method is called “area()”. The “Circle” class extends the “Shape” class and provides an implementation for the “area()” method.

An instance of the Circle class can be created, and its area() method can be called, but an instance of the Shape class cannot be created because it is abstract.

Note: Concrete subclasses must override an abstract method, and abstract classes may or may not contain abstract methods.

synchronized

In Java, synchronized is used to control multiple threads’ access levels to a method or block of code. In other words, only one method can simultaneously be accessed in a thread. 

This ensures that the shared resources accessed by the method or block of code are not modified by multiple threads simultaneously, which could lead to unexpected behavior.

				
					class Counter {
    private int count;

    synchronized void increment() {
        count++;
    }

    int getCount() {
        return count;
    }
}
				
			

In the above example, a class is defined named “Counter” with two methods, “increment” and “getCount”. The method “increment” is defined as synchronized; it can execute only one thread at a time.

If the method “increment” tries to execute multiple threads simultaneously, then the synchronized keyword will ensure that only one thread at a time “increment” method should be executed.


Note: synchronized keyword can also be used with blocks of code, not just methods. In this case, the code block is surrounded by the synchronized keyword and an object, and the code block is considered synchronized on that object’s monitor.

Please comment below if you need help with something in the above-discussed topic and have further questions.

Connect on:

Recent Articles