Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of "final class" in Java?

Tags:

java

final

I am reading a book about Java and it says that you can declare the whole class as final. I cannot think of anything where I'd use this.

I am just new to programming and I am wondering if programmers actually use this on their programs. If they do, when do they use it so I can understand it better and know when to use it.

If Java is object oriented, and you declare a class final, doesn't it stop the idea of class having the characteristics of objects?

like image 233
newbie Avatar asked Mar 03 '11 13:03

newbie


People also ask

What is the advantage of using final in Java?

Final keyword improves performance. Not just JVM can cache the final variables but also applications can cache frequently use final variables. 2. Final variables are safe to share in a multi-threading environment without additional synchronization overhead.

What is the benefit of final?

Final benefit means the amount payable to a beneficiary under the Defined Benefit Supplement Program upon the death of the member. Final benefit means the benefit granted once a final determination has been made as to the actual monthly amount payable by the retirement system.

What is the use of the final?

Using the final keyword means that the value can't be modified in the future. This entity can be - but is not limited to - a variable, a class or a method.

Is it good practice to use final in Java?

the reader of the code need not to reason at all about the value of a final variable (except for rare bad-code cases). So, yes, it's a good practice.


2 Answers

First of all, I recommend this article: Java: When to create a final class


If they do, when do they use it so I can understand it better and know when to use it.

A final class is simply a class that can't be extended.

(It does not mean that all references to objects of the class would act as if they were declared as final.)

When it's useful to declare a class as final is covered in the answers of this question:

  • Good reasons to prohibit inheritance in Java?

If Java is object oriented, and you declare a class final, doesn't it stop the idea of class having the characteristics of objects?

In some sense yes.

By marking a class as final you disable a powerful and flexible feature of the language for that part of the code. Some classes however, should not (and in certain cases can not) be designed to take subclassing into account in a good way. In these cases it makes sense to mark the class as final, even though it limits OOP. (Remember however that a final class can still extend another non-final class.)

like image 142
aioobe Avatar answered Oct 21 '22 16:10

aioobe


In Java, items with the final modifier cannot be changed!

This includes final classes, final variables, and final methods:

  • A final class cannot be extended by any other class
  • A final variable cannot be reassigned another value
  • A final method cannot be overridden
like image 30
andyqee Avatar answered Oct 21 '22 18:10

andyqee