Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reasoning for making a method final

Sorry, quick question here, just found something in my notes I don't understand in relation to making a method final. My notes claim you should make a method final for this reason :

Makes it impossible to enforce invariants.

A String should behave as a String.

I don't really understand what is meant by this. Can someone please break it down for me ? Thanks a lot.

like image 768
Albatross32 Avatar asked Apr 18 '11 21:04

Albatross32


People also ask

Why method params should be final?

final means you can't change the value of that variable once it was assigned. Meanwhile, the use of final for the arguments in those methods means it won't allow the programmer to change their value during the execution of the method. This only means that inside the method the final variables can not be reassigned.

Why would you want to make an entire class final?

The main purpose of using a class being declared as final is to prevent the class from being subclassed. If a class is marked as final then no class can inherit any feature from the final class. You cannot extend a final class.

What happens if a method is made final?

Whenever you make a method final, you cannot override it. i.e. you cannot provide implementation to the superclass's final method from the subclass. i.e. The purpose of making a method final is to prevent modification of a method from outside (child class).

What is the meaning of final method?

The final methods are the methods that cannot be overridden. So, these methods can't be overridden in child/subclasses.


2 Answers

I would guess that should have said "Make it possible to enforce invariants". Basically, if someone can override a method, then they can change behaviors affecting your class's invariants.

like image 111
Konstantin Komissarchik Avatar answered Oct 02 '22 19:10

Konstantin Komissarchik


There are typically two reasons to make a method final, performance and design. When a method is final, it may be inlined. Before HotSpot compiling (JDK 1.1.x), these methods were usually inlined at compile time, whereas with HotSpot they are inlined at runtime, unless the compiler can guarantee that the inlined method will always be compiled together with the code that uses it. There are two reasons I know for making a local variable or a parameter final. The first reason is that you don't want your code changing the local variable or parameter. It is considered by many to be bad style to change a parameter inside a method as it makes the code unclear. As a habit, some programmers make all their parameters "final" to prevent themselves from changing them. I don't do that, since I find it makes my method signature a bit ugly.

The second reason comes in when we want to access a local variable or parameter from within an inner class. This is the actual reason, as far as I know, that final local variables and parameters were introduced into the Java language in JDK 1.1. Source

like image 38
Vik Gamov Avatar answered Oct 02 '22 18:10

Vik Gamov