Get Started: Java Modifiers

In This Article, You Will Learn to know about Java Modifiers.

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

Table of Contents

Modifier

Modifier – Java Modifier determines how to use the methods and variables inside the class. 

In other word, the Java access modifier defines the classes that can be accessed by the class, its constructors, fields, and methods. Access modifiers can be defined independently for a particular class and its constructors, fields, and methods.

Types of Modifier

Java Modifiers

Java Static Modifier

Static Modifier – The Java Static variable & method is associated with its class name. 

A static method belongs to the class rather than the object of a class therefore static modifier never needs to create or declare any object to call static variable and method.

When a program starts executing that point of time there is no object resides in the memory and we need a point to start execution. As static method can be call without creation of object hence main() is static always. 

Important points to be remembered –

  • The Java static modifier allows a variable or a method to be associated with its class. 
  • A class cannot be declared as static. 
  • Since the static modifier is associated with its class name instead of object; we don’t need to create an object of the class to call static method or variable.
  • The variables and methods declared using static keyword are called class variables and class methods. 
  • We cannot access non-static data members from a static method because non-static data members are Instance variable.

Instance Variable – A variable that is an instance variable that is defined in a class, but is not part of methods, constructors or blocks. The instance variables appear after an object is created, and are available to all constructors, blocks, or methods in the class.

Java Final Modifier

Final Modifier – The Java final modifier is used to declare a variable as a constant variable. A variable defined with final keyword cannot be overridden, i.e., once a variable is defined with final keyword then its value cannot be changed.

Important points to be remembered –

  • The final keyword is used for security reasons.

  • The final modifier implements the following restrictions:
    • A final class can not be inherited.
    • A final method can not be changed (overridden) by a subclass.
    • A final data member can not be changed after initialization.
    • All methods and data members in a final class are implicitly final.

  • We can define constant using final keyword.
  • The complier forces us to initialize final variables in the declaration statement itself.
  • Making the entire class final is an extreme measure and is rarely necessary.

Java Abstract Modifier

Only abstract class can have abstract methods.

In other words, if the abstract method is defined within the default class, then the compiler will force the class to become the abstract class. 

A non-abstract class can not have abstract methods.

Restrictions

  • An abstract class can not be instantiated because the class is incomplete.

  • Instantiated – If you declare a class abstract, you cannot create any class object.

  • Subclasses must override the abstract methods of the superclass.

The java compiler forces to declare a class as abstract when:

  • A class has one or more abstract methods.

  • A class inherits an abstract method and does not override it.

Difference between abstract and final

  • The abstract modifier is the opposite of the final modifier.

In other words, if the class is final, you cannot inherit whereas the abstract method is needed to be inherited; otherwise, it will not be able to use the method of an abstract class. 

  • An abstract class must be subclassed. In comparison, a final class cannot be subclassed.

  • We can not use the final and abstract modifiers for the same class, method, or data member.

Important Points

  • Abstract methods have public scope. 
  • All abstract classes are public by default and cannot be instantiated. 
  • Constructor and static methods cannot be declared abstract.

Summary of Modifiers

Modifiers

Fields

Methods

static

Defines a class variable

Defines a class variable

final

Defines a constant 

The method cannot be overridden

abstract

Not applicable

No method body is defined. Its class must also be designed abstract

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Leave a Comment