0

Are there any benefits to finalizing a class and finalizing each individual variable and method inside that class? Or would that be redundant?

user3055593
  • 69
  • 1
  • 8

3 Answers3

0
  1. Finalizing a class prohibits from being extended meaning a class can not extend a final class
  2. Finalizing a variable means the value of the variable can not altered.
  3. 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

SpringLearner
  • 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
0

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

Community
  • 1
  • 1
0

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..

Michael Kazarian
  • 4,376
  • 1
  • 21
  • 25
  • 1
    There are reasons to use `final` keyword other than _improve performance_. – ADTC Dec 03 '13 at 08:35
  • @ADTC, answer have updated. – Michael Kazarian Dec 03 '13 at 08:59
  • 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