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
99
votes
7 answers

Why can final object be modified?

I came across the following code in a code base I am working on: public final class ConfigurationService { private static final ConfigurationService INSTANCE = new ConfigurationService(); private List providers; private…
Matt McCormick
  • 13,041
  • 22
  • 75
  • 83
95
votes
11 answers

Why would one declare an immutable class final in Java?

I read that to make a class immutable in Java, we should do the following, Do not provide any setters Mark all fields as private Make the class final Why is step 3 required? Why should I mark the class final?
Anand
  • 20,708
  • 48
  • 131
  • 198
92
votes
15 answers

Does using final for variables in Java improve garbage collection?

Today my colleagues and me have a discussion about the usage of the final keyword in Java to improve the garbage collection. For example, if you write a method like: public Double doCalc(final Double value) { final Double maxWeight = 1000.0; …
Goran Martinic
  • 3,807
  • 4
  • 21
  • 14
92
votes
2 answers

Final variable assignment with try/catch

Because I believe it is a good programming practice, I make all my (local or instance) variables final if they are intended to be written only once. However, I notice that when a variable assignment can throw an exception you cannot make said…
dtech
  • 13,741
  • 11
  • 48
  • 73
90
votes
8 answers

Initialize a static final field in the constructor

public class A { private static final int x; public A() { x = 5; } } final means the variable can only be assigned once (in the constructor). static means it's a class instance. I can't see why this is prohibited.…
Yaron Levi
  • 12,535
  • 16
  • 69
  • 118
88
votes
6 answers

Cannot reference "X" before supertype constructor has been called, where x is a final variable

Consider the following Java class declaration: public class Test { private final int defaultValue = 10; private int var; public Test() { this(defaultValue); // <-- Compiler error: cannot reference defaultValue before…
Amr Bekhit
  • 4,613
  • 8
  • 34
  • 56
86
votes
2 answers

In ArrayBlockingQueue, why copy final member field into local final variable?

In ArrayBlockingQueue, all the methods that require the lock copy it to a local final variable before calling lock(). public boolean offer(E e) { if (e == null) throw new NullPointerException(); final ReentrantLock lock = this.lock; …
mjlee
  • 3,374
  • 4
  • 27
  • 22
85
votes
7 answers

In Java, what purpose do the keywords `final`, `finally` and `finalize` fulfil?

In Java, what purpose do the keywords final, finally and finalize fulfil?
Pat R Ellery
  • 1,696
  • 3
  • 22
  • 40
83
votes
13 answers

what is the sense of final ArrayList?

Which advantages/disadvantages we can get by making ArrayList (or other Collection) final? I still can add to ArrayList new elements, remove elements and update it. But what is effect making it's final?
WelcomeTo
  • 19,843
  • 53
  • 170
  • 286
82
votes
8 answers

Will Java Final variables have default values?

I have a program like this: class Test { final int x; { printX(); } Test() { System.out.println("const called"); } void printX() { System.out.println("Here x is " + x); } public static…
user3766874
  • 793
  • 2
  • 6
  • 14
81
votes
7 answers

java: "final" System.out, System.in and System.err?

System.out is declared as public static final PrintStream out. But you can call System.setOut() to reassign it. Huh? How is this possible if it's final? (same point applies to System.in and System.err) And more importantly, if you can mutate the…
Jason S
  • 184,598
  • 164
  • 608
  • 970
80
votes
10 answers

Lambdas: local variables need final, instance variables don't

In a lambda, local variables need to be final, but instance variables don't. Why so?
Gerard
  • 2,784
  • 3
  • 19
  • 17
75
votes
4 answers

Strange Java behaviour with static and final qualifiers

In our team we found some strange behaviour where we used both static and final qualifiers. This is our test class: public class Test { public static final Test me = new Test(); public static final Integer I = 4; public static final…
srnjak
  • 915
  • 7
  • 21
74
votes
5 answers

Does final imply override?

As I understand it, the override keyword states that a given declaration implements a base virtual method, and the compilation should fail if there is no matching base method found. My understanding of the final keyword is that it tells the compiler…
quant
  • 21,507
  • 32
  • 115
  • 211
74
votes
11 answers

What's the point of a final virtual function?

Wikipedia has the following example on the C++11 final modifier: struct Base2 { virtual void f() final; }; struct Derived2 : Base2 { void f(); // ill-formed because the virtual function Base2::f has been marked final }; I don't understand…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662