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
51
votes
8 answers

Must all properties of an immutable object be final?

Must immutable objects have all properties be final? I would say they don't. But I don't know whether I am right or not.
DRastislav
  • 1,892
  • 3
  • 26
  • 40
50
votes
5 answers

Declaring an ArrayList object as final for use in a constants file

I am generating an ArrayList of objects. Following is the code ArrayList someArrayList = new ArrayList(); Public ArrayList getLotOfData() { ArrayList someData = new ArrayList(); return someData; } someArrayList = eDAO.getLotOfData(); Once I…
Raghu
  • 1,141
  • 5
  • 20
  • 39
47
votes
4 answers

Anonymous-Inner classes showing incorrect modifier

To my understanding, the following code should have printed true as output. However, when I ran this code it is printing false. From Java docs of Anonymous Classes 15.9.5. : An anonymous class is always implicitly final public class Test { …
T-Bag
  • 10,916
  • 3
  • 54
  • 118
47
votes
3 answers

In Java, can a final field be initialized from a constructor helper?

I have a final non-static member: private final HashMap myMap; I would like to initialize it using a method called by the constructor. Since myMap is final, my "helper" method is unable to initialize it directly. Of course I have…
csj
  • 21,818
  • 2
  • 20
  • 26
46
votes
4 answers

Java: Meaning of catch (final SomeException e)?

What does final do in the following Java expression? catch (final SomeExceptionType e)
Debajit
  • 46,327
  • 33
  • 91
  • 100
46
votes
2 answers

What's the point of declaring an object as "final"?

I just noticed that it's possible to declare objects as final in Scala: final object O What's the point of doing that? One cannot inherit from objects anyway: object A object B extends A // not found: type A
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
45
votes
5 answers

Why defining class as final improves JVM performance?

Quoting from http://sites.google.com/site/gson/gson-design-document: Why are most classes in Gson marked as final? While Gson provides a fairly extensible architecture by providing pluggable serializers and deserializers, Gson classes were…
Maxim Veksler
  • 29,272
  • 38
  • 131
  • 151
45
votes
4 answers

How to prevent a function from being overridden in Python

Is there a way to make a class function unoverriddable? Something like Java's final keyword. I.e, any overriding class cannot override that method.
olamundo
  • 23,991
  • 34
  • 108
  • 149
45
votes
4 answers

Java - Can final variables be initialized in static initialization block?

Based on my understanding of the Java language, static variables can be initialized in static initialization block. However, when I try to implement this in practice (static variables that are final too), I get the error shown in the screenshot…
Amit
  • 33,847
  • 91
  • 226
  • 299
45
votes
15 answers

On the static final keywords in Java

According to the tutorial: The static modifier, in combination with the final modifier, is also used to define constants. The final modifier indicates that the value of this field cannot change. I would agree with this only if the types involved…
Pippo
  • 1,543
  • 3
  • 21
  • 40
45
votes
11 answers

Final variable manipulation in Java

Could anyone please tell me what is the meaning of the following line in context of Java: final variable can still be manipulated unless it's immutable As far as I know, by declaring any variable as final, you can't change it again, then what…
Dusk
  • 2,191
  • 6
  • 38
  • 57
44
votes
9 answers

Java final abstract class

I have a quite simple question: I want to have a Java Class, which provides one public static method, which does something. This is just for encapsulating purposes (to have everything important within one separate class)... This class should neither…
Sauer
  • 1,429
  • 4
  • 17
  • 32
43
votes
10 answers

Is it a bad idea to declare a final static method?

I understand that in this code: class Foo { public static void method() { System.out.println("in Foo"); } } class Bar extends Foo { public static void method() { System.out.println("in Bar"); } } .. the static…
brasskazoo
  • 76,030
  • 23
  • 64
  • 76
41
votes
5 answers

What does Collections.unmodifiableSet() do in Java?

I can see that Collections.unmodifiableSet returns an unmodifiable view of the given set but I don't understand why we can't just use the final modifier to accomplish this. In my understanding, final declares a constant: something that cannot be…
Roman
  • 124,451
  • 167
  • 349
  • 456
40
votes
4 answers

Static Final Variable in Java

Possible Duplicate: private final static attribute vs private final attribute What's the difference between declaring a variable as static final int x = 5; or final int x = 5; If I only want to the variable to be local, and constant (cannot be…
darksky
  • 20,411
  • 61
  • 165
  • 254