In this article final keyword in java we give the information about In Java, final keyword is used to denote a constant. If we use final then we cannot change its value in future.

Final Keyword in Java:-

In Java, Final Keyword is used to restrict the user. It is a non-access modifier and can be used only in variables, classes and methods.

In Java, final keyword is used to denote a constant. If we use final then we cannot change its value in future.

In Java programming language, final is a predefined non-access modifier whose main purpose is to restrict access to classes, methods and variables. final keyword is used in three contexts –

  • variables
  • Methods
  • Classes

Final Variable

If we declare a variable with final keyword then we cannot change its value. It becomes constant. We should also initialize the variable while declaring it. If we do not initialize then it is called blank final keyword.

Blank final variable can be initialized inside the constructor and inside the static block. If we do not initialize the variable anywhere then we will get compile time error.

example:-

class Study

{

   final int capacity=150;

   void display()

{

      capacity=160;

   }

   public static void main(String args[])

{

      Study obj=new Study();

      obj.display();

   }

}

Output:- Compile Time Error

The only difference between final variable and normal variable is that we can assign value again to a normal variable whereas we cannot assign value again to final variable. Therefore, we should use final variable only when we want to keep the value constant.

Final Method –

When a method is declared with final keyword then it is called final method. A final method can never be overridden. This means that a child class can call the final method of the parent class but cannot override it.

example:-

class ABC

{

    public final void finalMethod()

{

        System.out.print(“this is ABC class”);

    }

}

class XYZ extends ABC

{

    public final void finalMethod()

{

        System.out.print(“this is XYZ class”);

    }

}

There will be a compile time error because we are overriding the finalMethod() method.

Final Class –

When a class is declared with final then it is called final class. We can never inherit a final class. That means we cannot extend it.

Example:-

final class FirstClass

 {

  public void show()

{

    System.out.println(“Hello world!”);

  }

}

// try to extend the final class

class SecondClass extends FirstClass

{

  public void show()

 {

    System.out.println(“How are you?”);

}

  public static void main(String[] args)

{

    SecondClass obj = new SecondClass();

    obj.show();

  }

}

There will be a compile time error in its output because we are trying to extend (inherit) FirstClass through SecondClass.

Important Points:-

  1. A constructor can never be declared as final.
  2. It is necessary to initialize the local final variable at the time of declaring it.
  3. All variables declared inside the interface are final by default.
  4. We cannot change the value of the final variable.
  5. Final method cannot be overridden.
  6. A final class cannot be inherited.
  7. If method parameters are declared as final then we cannot change the value of these parameters.
  8. final, finally, finalize are three different things, finally is used in exception handling and finalize is a method which is called by JVM during garbage collection.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *