Java Final keyword

Java Final Keyword

Java Final Keyword

Final keyword in java basically protects your information from unwanted modification. When a variable is initialized with some value and you don’t want others to modify it’s value again, then final keyword can be used at that time. In this tutorial, we will discuss about final keyword, how we can use it and some examples.

Java Final Keyword
Java Final Keyword

Final Keyword

Let’s understand final keyword with some real scenario.

You need to create a separate class for database connections. In this class, you are storing database details like user name, password and other connection details. Now, you want to make sure that your database details must be protected from unauthorized access and modification from external resources. In this case, what do you think should we do ?

So let’s create DbManager class.

class DbManager {
      private final String USER_NAME = "username";
      private final String PASSWORD = "password";
      private final String HOST = "your_host";
      private final String PORT = "port";
}

Look at the example above, we have declared variable as private that means nobody will be able to access it from outside of the class. We have declared it final, that means once the value of the variables is initialized it can not be modified.

Some important things to remember about final keyword

  1. Final keyword protects your information from modification.
  2. Once final variable is initialized with some value, it can never be changed.
  3. Final keyword can be used with class, instance variables and methods.
  4. When used with methods, that method can not be overridden.
  5. When used with class, that class can not be inherited.
  6. Final variable without any value is called “Blank Final Variable”.
  7. You can initialize blank final variable through a constructor or initializer block.
  8. Final variable can be static, you can assign value to static final variable using static initializer block.
  9. You can also use final with parameters.


Final Variable

Final keyword can be used with variables. When used with the variable, it’s value can not be changed once initialized. Or we can say, you can create constant variables via final keyword.

Example

class Demo {
      private final String URL = "https://www.google.com";
      
      public String getURL() {
         return this.URL;
      }

      public static void main(String arg[]) {
             Demo d = new Demo();
             System.out.println(d.getURL());
      }
}
// If you will try to modify it's value like this, you will get error.

class Demo {
      private final String URL = "https://www.google.com";

      public void changeURL() {
         this.URL = "https://www.facebook.com";
      }
      
      public String getURL() {
         return this.URL;
      }

      public static void main(String arg[]) {
             Demo d = new Demo();
             d.changeURL(); // you will get error.
             System.out.println(d.getURL());
      }
}

Final Methods

Final can also be used with methods. When we use final keyword with method, that method can not be overridden (We will discuss about overridden method later, method overriding is a concept in which child class provides it’s own definition instead of using base class definition). So when we use final with method, that means child class will not be able to provide it’s own definition. Child class will have to use the method of base class.

Example

class Parent {
      public final void showMessage() {
          System.out.println("Message from parent.");
      }
}

class Child extends Parent {
     public void showMessage() { // You can not override showMessage, it's final.
         System.out.println("My own message."); // Not possible with final method.
     }
}

Final Class

A class can also be final. Basically we use final class when we don’t want anybody to inherit that class. We can restrict a class from inheritance by using final keyword. When final keyword is used with class that means you cannot extend it in your child class.

Example

final class Parent {

}

class Child extends Parent { // Not possible.

}

Blank Final Variable

Uninitialized final variable is called blank final variable. You can initialize value of blank final variable through a constructor or initializer block.

Example

class Demo {
    private final String message;

    public Demo() {
          this.message = "Initialized in constructor.";
    }

    public String getMessage() {
          return this.message;
    }

    public static void main(String arg[]) {
          Demo d = new Demo();
          System.out.println(d.getMessage()); // Initialized in constructor.
    }
}

Static Blank Final Variable

You can declare final variable as static. Static final variable which is not initialized is called “Static Blank Final Variable”, you can initialize it’s value using static initializer block.

One of the benefits of using static final variable is, you can make constant values. These constant values can be accessed via class name. You don’t need any object to access these values in case of public static final variables.

Example

public class Demo{
 
 private static final String message;
 
 static {
   message = "Initialized inside static block.";
 }
 
 public String getMessage() {
   return this.message;
 }

 public static void main(String[] args) {

   Demo d = new Demo();
   System.out.println(d.getMessage()); // Initialized inside static block.

 }
}

Leave a Reply

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