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

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

  • Check if Object is null Java
    14 March

    Check if Object Is Null in Java

    Table of ContentsComparison Operator to Check if Object Is Null in JavaisNull() Method to Check if Object Is Null in JavanonNull() Method to Check if Object Is Null in JavarequireNonNull() Method to Check if Object Is Null in JavaConclusion An object in Java is an instance of a class. It is a real entity existing […]

  • How to print multiple variables in java
    31 January

    How to Print Multiple Variables in Java

    Table of ContentsWays to Print Multiple Variables in JavaUsing System.out.printUsing System.out.printfPrint Multiple Variables in Java Using LoggerFrequently Asked QuestionsHow Do I Print Multiple Values on One Line in JavaHow to Print Multiple Integers in JavaHow to Print String and Integer in Same Line in Java In this post, we will see how to print multiple […]

  • What is == in java
    01 December

    What is == in java

    Table of ContentsWhat is meaning of == in JavaCode example of ==Object equality using equal to ==The difference between == and equals== in EnumConclusion In this tutorial, we will learn what is == in Java as it is a common lexeme that we will use while developing applications. The == lexeme can be confused with […]

  • Multiple classes in one file in Java
    18 October

    Multiple classes in one file in Java

    Table of ContentsIntroductionMethods to Implement Multiple Classes In One Java Program1) Nested classes2) Multiple non-static nested classes In this post, we will see how to have multiple classes in one file in java. Introduction You need to have any number of classes in a single Java file, but there is a restriction that you can […]

  • Break out of nested loop in java
    22 July

    How to break out of nested loops in Java

    Table of ContentsUsing break (will break inner loop)Using named loopUsing named blockUsing returnConclusion In this post, we will see how to break out of nested loops in Java. Using break (will break inner loop) It is very important to understand how nested loops work to ensure that applying break will output the desired result. If […]

  • public static void main(String args[]) - Java main method
    11 June

    public static void main(String[] args) – Java main method

    Table of ContentsWhy is the main method so important?SyntaxExplanation of the KeywordsExamples of Java main method ProgramsSimple Java ProgramPassing arguments in the main methodNon-Public main method.Defining a return value for the main methodOverloading the main methodQuestionsCan we run java without main method?Can we have more than one main method in classConclusion If you have worked […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.