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

Difference between final static and static final

I found a code where it declared code like private final static String API_RTN_SUCCESS = "0"; private final static String API_RTN_ERROR = "1"; public static final String SHARED_PREFERENCE_CONFIG = "shared_preference_config"; public static final…
MBMJ
  • 5,323
  • 8
  • 32
  • 51
187
votes
6 answers

Is final ill-defined?

First, a puzzle: What does the following code print? public class RecursiveStatic { public static void main(String[] args) { System.out.println(scale(5)); } private static final long X = scale(10); private static long…
Little Helper
  • 1,870
  • 3
  • 12
  • 20
185
votes
9 answers

final keyword in method parameters

I often encounter methods which look like the following: public void foo(final String a, final int[] b, final Object1 c){ } What happens if this method is called without passing it final parameters. i.e. an Object1 that is later changed (so is not…
Aly
  • 15,865
  • 47
  • 119
  • 191
161
votes
11 answers

Java's final vs. C++'s const

The Java for C++ programmers tutorial says that (highlight is my own): The keyword final is roughly equivalent to const in C++ What does "roughly" mean in this context? Aren't they exactly the same? What are the differences, if any?
WinWin
  • 7,493
  • 10
  • 44
  • 53
155
votes
8 answers

Why is there no Constant feature in Java?

I was trying to identify the reason behind constants in Java I have learned that Java allows us to declare constants by using final keyword. My question is why didn't Java introduce a Constant (const) feature. Since many people say it has come from…
gmhk
  • 15,598
  • 27
  • 89
  • 112
152
votes
5 answers

Java `final` method: what does it promise?

In a Java class a method can be defined to be final, to mark that this method may not be overridden: public class Thingy { public Thingy() { ... } public int operationA() {...} /** this method does @return That and is final. */ …
towi
  • 21,587
  • 28
  • 106
  • 187
145
votes
6 answers

How do I initialize a final class property in a constructor?

In Java you are allowed to do this: class A { private final int x; public A() { x = 5; } } In Dart, I tried: class A { final int x; A() { this.x = 5; } } I get two compilation errors: The final…
Blackbam
  • 17,496
  • 26
  • 97
  • 150
145
votes
16 answers

Why is the String class declared final in Java?

From when I learned that the class java.lang.String is declared as final in Java, I was wondering why that is. I didn't find any answer back then, but this post: How to create a replica of String class in Java? reminded me of my query. Sure, String…
Alex Ntousias
  • 8,962
  • 8
  • 39
  • 47
142
votes
8 answers

Behaviour of final static method

I have been playing around with modifiers with static method and came across a weird behaviour. As we know, static methods cannot be overridden, as they are associated with class rather than instance. So if I have the below snippet, it compiles…
Harish
  • 7,589
  • 10
  • 36
  • 47
126
votes
4 answers

Getting value of public static final field/property of a class in Java via reflection

Say I have a class: public class R { public static final int _1st = 0x334455; } How can I get the value of the "_1st" via reflection?
Viet
  • 17,944
  • 33
  • 103
  • 135
120
votes
5 answers

Is there any performance reason to declare method parameters final in Java?

Is there any performance reason to declare method parameters final in Java? As in: public void foo(int bar) { ... } Versus: public void foo(final int bar) { ... } Assuming that bar is only read and never modified in foo().
Kip
  • 107,154
  • 87
  • 232
  • 265
116
votes
11 answers

Making java method arguments as final

What difference that final makes between the code below. Is there any advantage in declaring the arguments as final. public String changeTimezone( Timestamp stamp, Timezone fTz, Timezone toTz){ return .... } public String changeTimezone(final…
John
  • 2,682
  • 5
  • 23
  • 24
105
votes
7 answers

Why are all fields in an interface implicitly static and final?

I am just trying to understand why all fields defined in an Interface are implicitly static and final. The idea of keeping fields static makes sense to me as you can't have objects of an interface but why they are final (implicitly)? Any one knows…
peakit
  • 28,597
  • 27
  • 63
  • 80
104
votes
2 answers

Why are `private val` and `private final val` different?

I used to think that private val and private final val are same, until I saw section 4.1 in Scala Reference: A constant value definition is of the form final val x = e where e is a constant expression (§6.24). The final modifier must be present…
Yang Bo
  • 3,586
  • 3
  • 22
  • 35
103
votes
2 answers

Effectively final vs final - Different behavior

So far I thought that effectively final and final are more or less equivalent and that the JLS would treat them similar if not identical in the actual behavior. Then I found this contrived scenario: final int a = 97; System.out.println(true ? a :…
Zabuzard
  • 25,064
  • 8
  • 58
  • 82