Questions tagged [final]

final is a common keyword specifying that the reference declared as final cannot be modified once it is initialized. In Java the keyword final is roughly equivalent to const in C++. In C++ the final keyword allows you to declare a virtual method, override it N times and then mandate that 'this can no longer be overridden'.

Refer to how the final keyword is used in different programing languages

1662 questions
683
votes
25 answers

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

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,…
newbie
  • 14,582
  • 31
  • 104
  • 146
564
votes
14 answers

Change private static final field using Java reflection

I have a class with a private static final field that, unfortunately, I need to change it at run-time. Using reflection I get this error: java.lang.IllegalAccessException: Can not set static final boolean field Is there any way to change the…
fixitagain
  • 6,543
  • 3
  • 20
  • 24
558
votes
20 answers

How does the "final" keyword in Java work? (I can still modify an object.)

In Java we use final keyword with variables to specify its values are not to be changed. But I see that you can change the value in the constructor / methods of the class. Again, if the variable is static then it is a compilation error. Here is the…
G.S
  • 10,413
  • 7
  • 36
  • 52
478
votes
17 answers

What is the difference between the "const" and "final" keywords in Dart?

What is the difference between the const and final keywords in Dart?
Ishmal Ijaz
  • 5,317
  • 3
  • 11
  • 17
452
votes
12 answers

Why should I use the keyword "final" on a method parameter in Java?

I can't understand where the final keyword is really handy when it is used on method parameters. If we exclude the usage of anonymous classes, readability and intent declaration then it seems almost worthless to me. Enforcing that some data…
Yotam Gilboa
412
votes
14 answers

Does use of final keyword in Java improve the performance?

In Java we see lots of places where the final keyword can be used but its use is uncommon. For example: String str = "abc"; System.out.println(str); In the above case, str can be final but this is commonly left off. When a method is never going…
Abhishek Jain
  • 6,296
  • 7
  • 26
  • 34
386
votes
15 answers

Difference between final and effectively final

I'm playing with lambdas in Java 8 and I came across warning local variables referenced from a lambda expression must be final or effectively final. I know that when I use variables inside anonymous class they must be final in outer class, but still…
alex
  • 10,900
  • 15
  • 70
  • 100
334
votes
22 answers

private final static attribute vs private final attribute

In Java, what's the difference between: private final static int NUMBER = 10; and private final int NUMBER = 10; Both are private and final, the difference is the static attribute. What's better? And why?
okami
279
votes
11 answers

Should a "static final Logger" be declared in UPPER-CASE?

In Java, static final variables are constants and the convention is that they should be in upper-case. However, I have seen that most people declare loggers in lower-case which comes up as a violation in PMD. e.g: private static final Logger logger…
dogbane
  • 266,786
  • 75
  • 396
  • 414
251
votes
20 answers

Cannot refer to a non-final variable inside an inner class defined in a different method

Edited: I need to change the values of several variables as they run several times thorugh a timer. I need to keep updating the values with every iteration through the timer. I cannot set the values to final as that will prevent me from updating the…
Ankur
  • 50,282
  • 110
  • 242
  • 312
224
votes
6 answers

Comparing strings with == which are declared final in Java

I have a simple question about strings in Java. The following segment of simple code just concatenates two strings and then compares them with ==. String str1="str"; String str2="ing"; String…
Tiny
  • 27,221
  • 105
  • 339
  • 599
212
votes
5 answers

Final arguments in interface methods - what's the point?

In Java, it is perfectly legal to define final arguments in interface methods and do not obey that in the implementing class, e.g.: public interface Foo { public void foo(int bar, final int baz); } public class FooImpl implements Foo { …
mindas
  • 26,463
  • 15
  • 97
  • 154
203
votes
11 answers

Good reasons to prohibit inheritance in Java?

What are good reasons to prohibit inheritance in Java, for example by using final classes or classes using a single, private parameterless constructor? What are good reasons of making a method final?
cretzel
  • 19,864
  • 19
  • 58
  • 71
198
votes
11 answers

What is the purpose of the "final" keyword in C++11 for functions?

What is the purpose of the final keyword in C++11 for functions? I understand it prevents function overriding by derived classes, but if this is the case, then isn't it enough to declare as non-virtual your final functions? Is there another thing…
lezebulon
  • 7,607
  • 11
  • 42
  • 73
196
votes
15 answers

When should one use final for method parameters and local variables?

I've found a couple of references (for example) that suggest using final as much as possible and I'm wondering how important that is. This is mainly in the the context of method parameters and local variables, not final methods or classes. For…
eaolson
  • 14,717
  • 7
  • 43
  • 58
1
2 3
99 100