Final keyword in java with example

In this post, we will see about final keyword in java. Final keyword can be associated with:

Final is often used when you want restrict others from doing any changes.

Table of Contents

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

Let’s go through each by one.


Final variable

If you make any variable final then you are not allowed to change its value later.It will be constant.If you try to change value, then compiler will give you error.
Let’s take a simple example:
In above example, you will get compilation error with text “The final field FinalExampleMain.count cannot be assigned” at highlighted row.

Blank final variable

Blank final variable is the variable which is not initialised at the time of declaration. It can be initialised only in constructor.
But if you do not initialise final variable, you will get compilation error as below.
In above example, you will get compilation error with text “The blank final field count may not have been initialised” at highlighted row.
You can initialize final variable once in a constructor as below.
Above code will work fine. You might be thinking what may be use of it.
Let’s say you have Employee class and it has an attribute called empNo. Once object is created, you don’t want to change empNo.
So you can declare it final and initialize it in constructor.

static blank final variable

static blank final variable is static variable which is not initialized at the time of declaration. It can be initialized only in static block.

When you run above program, you will get below output:

10

Final method

You can not override final methods in subclasses. You can call parent’s class final method using subclass’s object but you can not override it.

You will get compilation error at highlighted row with text "Cannot override the final method from Shape".

If you remove draw method from rectangle class, it will work fine.

When you run above program, you will get below output:

Draw method in shape class

Final class

If you declare a class final, no other class can extend it.

You will get compilation error at highlighted row with text "The type Rectangle cannot subclass the final class Shape".


Summary for final keyword

  1. You can use final keyword with variable, method, and class.
  2. You can not use final keyword with constructor.
  3. If you do not initialize final variable(unless assigned in constructor), you will get compilation error.
  4. You can not change value of final variable once initialized.
  5. You can not override final methods in the sub class.
  6. You can not extend any final class.

You may also like:
Static keyword in java with examples

Related Posts

  • Java projects for beginners
    30 April

    Top 20 Java Projects for Beginners

    This tutorial provides the top 20 java projects for beginners for practice. These projects will help you to learn java quickly and efficiently.

  • How to end program in java
    08 March

    How to end program in java

    Ho In this post, we will see how to end program in java. You can use System.exit(0) to end program in java. Using System.exit() to end java program You can use exit() method of System class to end java program. System.exit() causes Java virtual machine to exit and terminate the current process. You can call […]

  • 25 September

    Reference Variable in Java

    In this post, we will see about Reference variable in java. Reference variable A variable that holds reference of an object is called a reference variable. Variable is a name that is used to hold a value of any type during program execution. If the type is an object, then the variable is called reference […]

  • 26 March

    Global variables in java

    In this post, we will see how to define global variables in java. Unlike C/C++, there are no standard global variables in java but you can still define global variables that can be used across all classes. Global variables are those variables that can be accessed across all the classes. Java does not support global […]

  • 26 March

    Static block in java

    In this post, we will see about how to implement static block in java. Here are some important points about static block in java. Static block is used for static initializations of a class Static block is executed only once when either you create an object of the class or access static member of the […]

  • 26 August

    Java default constructor

    In this post, we will see about Java default constructor. Default constructor is the no arg constructor which is inserted by compiler unless you provide any other constructor explicitly. You won’t able to see it as it is present in class file rather than source file. Is there any difference between no argument constructor and […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.