Are there any benefits to finalizing a class and finalizing each individual variable and method inside that class? Or would that be redundant?
-
do you know what final means? – shreyansh jogi Dec 03 '13 at 08:30
-
Immutability. Learn about final key word. You would know why it is useful. – Suresh Atta Dec 03 '13 at 08:30
-
@SURESH Does immutability always means constness? – Lews Therin Dec 03 '13 at 08:31
-
what is point? what is your actual question? Do you want to know about final class? or final variable? – Murali Dec 03 '13 at 08:32
-
@LewsTherin The state of the object can change, But not the object it self to refer something else. It's something different in java than c++. http://stackoverflow.com/questions/19049697/why-final-instance-class-variable-in-java/19049753#19049753 – Suresh Atta Dec 03 '13 at 08:37
3 Answers
- Finalizing a class prohibits from being extended meaning a class can
not extend a
final
class - Finalizing a variable means the value of the variable can not altered.
- Finalizing a method means it can not be overidden.
So the benefits are dependent on how you use those
As the final class can not be extended but creating of instances is not prohibited.For example String
class.You can create an instance of String class but you can not extend that.
So if a variable in final class is not made final then you can alter the values also

- 13,738
- 20
- 78
- 116
-
I understand that. So it wouldn't be redundant to make a variable final inside a class that has been made final? – user3055593 Dec 03 '13 at 08:34
-
No, the two finals have different meanings. The variables inside a final class are not automatically final. They can still be altered. Also, the methods inside a final class are not automatically final. They can be overridden by an implementation. – ADTC Dec 03 '13 at 08:36
-
@user3055593 final classes can not extended but you can create an instance of the final class and after that you can call the respective variables.So if the variables are not final then you can alter the values – SpringLearner Dec 03 '13 at 08:38
It's not redundant. They have different uses
Marking a class as final means that it cannot be inherited, but it otherwise can behave like a regular class. See Use of final class in Java
Marking members as final means that their values cannot be changed. See http://javarevisited.blogspot.com/2011/12/final-variable-method-class-java.html

- 1
- 1

- 76
- 3
Theoretically, it would little improve performance. But on practice you will not see it. Therefore it will excessive.
But you will get other useful possibilities. You will can't inherite from finalized class; you can't override finalized methods; you can only initialize finalized field. In all cases final
means something that never changes.
Also final
is useful for multithreading tasks - if multiple threads compute something, it is good practice to create a new immutable object from the old than to change old and write synchronization..

- 4,376
- 1
- 21
- 25
-
1There are reasons to use `final` keyword other than _improve performance_. – ADTC Dec 03 '13 at 08:35
-
-
Okay =) You can break it into nice paragraphs and expand more :).. All the answers here seem to be the same thing, pretty much - although you add more about multithreading which the other answers don't have. Good work :D – ADTC Dec 03 '13 at 09:07
-
@ADTC, I have desire to write some articles about certain java features (generics vs wildcards, use annotation for declarative style). Many coders have problems with understanding it. But I don't know good platform for it. For example I want similar to [this platform](http://habrahabr.ru/post/203026/), but not russian. – Michael Kazarian Dec 03 '13 at 09:27
-
That looks like a blog. Why don't you just make a new blog? Wordpress or Blogger should be a good start. – ADTC Dec 03 '13 at 09:29
-
@ADTC, it very tematical blog (about IT relation things) with code highlighting and big community (and strange politics). It good when I can see discussion. But it has some lacks - russian only and invites. – Michael Kazarian Dec 03 '13 at 09:39
-
I'm sure you can Google a solution for it. But I'm sorry I don't have experience in having a programmer-optimized blog. – ADTC Dec 03 '13 at 09:41
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42396/discussion-between-michael-kazarian-and-adtc) – Michael Kazarian Dec 03 '13 at 10:06
-
@ADTC codeproject appropriate for it ([examle](http://www.codeproject.com/Articles/691214/Validating-incoming-JSON-using-Upida-Net)). I will test it. – Michael Kazarian Dec 03 '13 at 10:10