What you read in this article:


My name is Arsalan Mirbozorgi and I’m trying to introduce you to the final keyword in this article and explain its use in the Java programming language. Java is one of the most popular and appropriate programming languages ​​that we have previously published articles about.

Final Keywords

In Java, the final keyword is used to define classes, methods, variables, and other fields.

Class definition

If you use Final in one class, there will be no inheritance in that other class. In fact, using the Final in a class, you lose one of the most important features of the Java programming language.

Method definition

If you use Final with the method, you can no longer override this method and change the way that method does its job.

Define a variable

If you use the final keyword to name a variable with an initial value, this variable has a final value and you cannot change its value.

You can define variables with an initial constant value with the final and static keywords. These two keywords are different from each other, and the difference is that static variables can access the object generated from the class independently.

In order to better understand how the final keyword works in Java, it is better to give you another example.

To better understand, you need to create a project called Final and place it in a class called WhatIsFinal.

Note that when constructing this class, you should not tick the Public Static Void Main option. This key is not in this executable class. Your source code is as follows.

 public class WhatIsFinal { }

Next you need to create a method in this class. 

The function of this method is to show the last name and date of birth of each user. To do this, you must enter the following code.

public class WhatIsFinal { public void showData() { String name = "Arsalan"; String lastName = "Mirbozorgi"; int dateOfBirth = 1362; System.out.println(name + "\n" + lastName + "\n" + dateOfBirth); } }

In this kind of code, a method is defined which is named showData(), then created two objects from the String class named “name” and “lastName ”. Finally, using the int data type, we defined a variable called dateOfBirth. We assigned the string values ​​”Arsalan” and “Mirbozorgi” and the number “1365” to each of them. To show this information in the console section, we used the println() method and referenced each of the variables we mentioned as an input argument. If we do that, it will print the values ​​assigned to each on the console. We use the \n sign to print each piece of information related to name, surname and year of birth in separate lines. In this case, this information is shown in three separate lines:

We create another class called ActionClass inside the same project and this time we tick the public static void main option because we want this class to be our executable class. The code for this class is like that:

public class ActionClass { public static void main(String[] args) { } }

In this part, we should create an object from the What Is Final class in the ActionClass class. Then, the object we made, we have access to the method we defined in that class, and we can call it. The code for this act is written like that: 

public class ActionClass {
    public static void main(String[] args) {
        WhatIsFinal objectOne = new WhatIsFinal();

In this code, we should create an object named objectOne from the WhatIsFinal class, and in the next line, we call the showData () method of this class and on the mentioned object. Then, we will have following output: 


In Java, to inherit a class from another class, we must first use “extend” and after that write the class name, and then write the name of the class we want to inherit from.

In this case, the Change class has all the features of the What Is Final class. Next we want to change some of the features that the Change class inherited from the WhatIsFinal class.

For that, we need to override the behavior of the showData() method of the What Is Final class in the Change class, in which case we will have the following result:

public class Change extends WhatIsFinal { @Override public void showData() { String name = "Ali"; String lastName = "Asadi"; int dateOfBirth = 1367; System.out.println(name + "\n" + lastName + "\n" + dateOfBirth); } }

For the override the showData() method, we must first put the @Override command and then write the method name and then change the values ​​assigned to the variables defined in the showData() method, and then, in the WhatIsFinal class, we should change “Ali”, “Asadi”, and  “1367”.

Then we go back to the ActionClass class and in that class we create a new object from the Change class.

Then we will have that: 

public class ActionClass { public static void main(String[] args) { WhatIsFinal objectOne = new WhatIsFinal(); objectOne.showData(); Change objectTwo = new Change(); objectTwo.showData(); } }

In this code, an object is created from the Change class called objectTwo, and in the following we call the override method showData() of this class on the created object.  The output is like that: 


After this step, the information related to both objects is printed in separate lines. Up to now we have talked about Final and its use in the Java programming language.

We have created a class called WhatIsFinal and added our desired attributes along with a method for displaying them.

After that, we defined the class as the starting point of the program and also created an object from the WhatIsFinal class in it and called the method defined in this class on this object. 

After that, we defined another class called Change so that this class inherits from the WhatIsFinal class.

Next, we override the behavior of the showData () method of the WhatIsFinal class in this class.

After this we can test the effect of the final keyword on the program execution process.

As we told you at the beginning of this article, the term final is used in a variety of contexts, such as defining classes and methods and variables.

You can use this keyword to define a constant value for variables, and in addition to this in the naming of methods, you can avoid the possibility of overwriting the method.

And finally

Mirbozorgi website is trying to help you learn a variety of content with practical articles. If you have any questions or concerns, please email me.

Leave A Comment

53 − = 51

Please Send Email

Your message sent successfully
There has been an error