Questions tagged [final]

22 questions
136
votes
4 answers

Excessive use "final" keyword in Java

While I understand what the final keyword is used for in the context of classes and methods as well as the intent of its use for in regards to variables; however, the project I just started working on seems to have an excessive number of them and…
rjzii
  • 11,274
  • 6
  • 46
  • 71
132
votes
5 answers

In Java, should I use "final" for parameters and locals even when I don't have to?

Java allows marking variables (fields / locals / parameters) as final, to prevent re-assigning into them. I find it very useful with fields, as it helps me quickly see whether some attributes - or an entire class - are meant to be immutable. On the…
Oak
  • 5,215
  • 6
  • 28
  • 39
80
votes
7 answers

Why declare final variables inside methods?

Studying some classes of Android, I realized that most of the variables of methods are declared as final. Example code taken from the class android.widget.ListView: /** * @return Whether the list needs to show the top fading edge */ private boolean…
Rodrigo
  • 1,077
  • 1
  • 9
  • 10
55
votes
10 answers

Why would the 'final' keyword ever be useful?

It seems Java has had the power to declare classes not-derivable for ages, and now C++ has it too. However, in the light of the Open/Close principle in SOLID, why would that be useful? To me, the final keyword sounds just like friend - it is legal,…
Vorac
  • 7,073
  • 7
  • 38
  • 58
37
votes
7 answers

Why is using 'final' on a class really so bad?

I am refactoring a PHP OOP legacy website. I am so tempted to start using 'final' on classes to "make it explicit that the class is currently not extended by anything". This might save lots of time if I come to a class and I am wondering if I can…
JW01
  • 3,579
  • 4
  • 22
  • 22
33
votes
2 answers

Naming convention: Final fields (not static)

Today I had a discussion with a co-worker about the naming of final fields in Java classes. In his opionion final fields should also be considered constants since their values won't change after the creation of the instance. This would lead to the…
Sascha Wolf
  • 441
  • 1
  • 4
  • 6
17
votes
2 answers

Declaring a class final?

I like my code to be written well; however, I have run into not really an problem, but more of a question about conventions. Say I have this class. public class Test { public void doStuff() { System.out.println("stuff"); } } This…
Jake Anderson
  • 181
  • 1
  • 4
16
votes
3 answers

In C++, when should I use final in virtual method declaration?

I know that final keyword is used to prevent virtual method from being overriden by derived classes. However, I can't find any useful example when I should really use final keyword with virtual method. Even more, it feels like usage of final with…
metamaker
  • 273
  • 2
  • 10
14
votes
5 answers

Why is the 'final' keyword used so little in the industry?

Since I discovered the powers of the final keyword in Java a few years ago, it helped me to make my codes a lot more readable, since people can easily see what are read-only variables. It may also provide a little boost to the JIT, and while that's…
Aurelien Ribon
  • 251
  • 2
  • 6
12
votes
3 answers

What are the pros and cons of using final methods (in abstract classes)

For the purpose of writing a coding styleguide, how should final methods in software design be judged? By final I mean the Object-oriented sense that a class that can be subclassed provides methods, and that class using special syntax to prevent…
tkruse
  • 246
  • 2
  • 11
8
votes
2 answers

Why to declare a String (as final) and then use it?

In a typical spring mvc validator class, while inserting an errorCode value in the Errors object, what difference does it make between using a String (props.somefield.req) like so errors.rejectValue("elementId", "props.somefield.req"); verses a…
happybuddha
  • 201
  • 1
  • 3
  • 7
5
votes
1 answer

Declaring functions as final... except when it is me who does the deriving

I have a class in which I want to disallow other programmers from overriding one of it's methods, since it requires special knowledge of the inner workings of the class. Since I personally know how the class works, I would like to be able to make an…
DudeOnRock
  • 1,079
  • 10
  • 21
4
votes
2 answers

Java: using final keyword only on method parameters that expect immutable objects?

I came across the interesting topic about final method parameters and that they essentially don't provide much advantages. I wondered if it would be sensible to mark only those method parameters as final, when it is advisable to pass an immutable…
Unknown Id
  • 221
  • 2
  • 5
4
votes
3 answers

Java: How to make local fields & parameters final without having a 'final' keyword on each declaration

By default, I want all my local variables and method parameters to be final. Unfortunately, the Java language designers chose a different default: variables¶meters are by default non-final, and final variables¶meters need to be declared with…
oberlies
  • 466
  • 4
  • 16
3
votes
2 answers

Explicit type and final in stream lambdas

This argument has been going on for weeks: Bot.getGuild().getMembers().stream() .filter((final Member m) -> m.getRoles().size() == 0) .filter((final Member m) -> !m.getUser().isBot()) …
1
2