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
67
votes
5 answers

When to use Final in PHP?

I know what the definition is of a Final class, but I want to know how and when final is really needed.
Inga Johansson
  • 771
  • 1
  • 5
  • 8
67
votes
6 answers

final transient fields and serialization

Is it possible to have final transient fields that are set to any non-default value after serialization in Java? My usecase is a cache variable — that's why it is transient. I also have a habit of making Map fields that won't be changed (i.e.…
user319799
66
votes
5 answers

Why inner class can override private final method?

I wondered if it makes sense to declare a private method as final as well, and I thought it doesn't make sense. But I imagined there's an exclusive situation and wrote the code to figure it out: public class Boom { private void touchMe() { …
chicout
  • 937
  • 7
  • 16
65
votes
9 answers

Why can I edit the contents of a final array in Java?

The following code in Java uses a final array of String. final public class Main { public static final String[] CONSTANT_ARRAY = {"I", "can", "never", "change"}; public static void main(String[] args) { for (int x = 0; x <…
Lion
  • 18,729
  • 22
  • 80
  • 110
62
votes
13 answers

when exactly are we supposed to use "public static final String"?

I have seen much code where people write public static final String mystring = ... and then just use a value. Why do they have to do that? Why do they have to initialize the value as final prior to using it? UPDATE Ok, thanks all for all your…
storm_buster
  • 7,362
  • 18
  • 53
  • 75
61
votes
7 answers

'public static final' or 'private static final' with getter?

In Java, it's taught that variables should be kept private to enable better encapsulation, but what about static constants? This: public static final int FOO = 5; Would be equivalent in result to this: private static final int FOO = 5; ... public…
Chris Cummins
  • 935
  • 1
  • 8
  • 12
60
votes
2 answers

How does "final int i" work inside of a Java for loop?

I was surprised to see that the following Java code snippet compiled and ran: for(final int i : listOfNumbers) { System.out.println(i); } where listOfNumbers is an array of integers. I thought final declarations got assigned only once. Is the…
Abe
  • 2,463
  • 4
  • 19
  • 16
59
votes
3 answers

Is there an equivalent to "sealed" or "final" in TypeScript?

I'm trying to implement a method in a super class that should be available for use, but not changeable, in sub classes. Consider this: export abstract class BaseClass { universalBehavior(): void { doStuff(); // Do some universal stuff…
bubbleking
  • 3,329
  • 3
  • 29
  • 49
59
votes
12 answers

Could a final variable be reassigned in catch, even if assignment is last operation in try?

I am quite convinced that here final int i; try { i = calculateIndex(); } catch (Exception e) { i = 1; } i cannot possibly have already been assigned if control reaches the catch-block. However, Java compiler disagrees and claims the final local…
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
59
votes
2 answers

What is the c# equivalent of public final static in java

In Java I can write: public final static MyClass foo = new MyClass("foo"); Is there any equivalent in C#?
peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217
57
votes
1 answer

Why declare a function argument to be final?

I'm currently working my way through the book "Teach Yourself Android Application Development in 24 Hours" published by Sams. I'm relatively new to Java, Android or otherwise. I have a very solid background in ActionScript 3, which has enough…
scriptocalypse
  • 4,942
  • 2
  • 29
  • 41
57
votes
12 answers

Why would one mark local variables and method parameters as "final" in Java?

In Java, you can qualify local variables and method parameters with the final keyword. public static void foo(final int x) { final String qwerty = "bar"; } Doing so results in not being able to reassign x and qwerty in the body of the…
Julien Chastang
  • 17,592
  • 12
  • 63
  • 89
57
votes
3 answers

Can a static method be overridden in C#?

I was told that static methods are implicitly final and therefore can't be overridden. Is that true? Can someone give a better example of overriding a static method? If static methods are just class methods, what is the real use of having them?
aspiring
  • 1,557
  • 2
  • 20
  • 43
55
votes
4 answers

Changing private final fields via reflection

class WithPrivateFinalField { private final String s = "I’m totally safe"; public String toString() { return "s = " + s; } } WithPrivateFinalField pf = new WithPrivateFinalField(); System.out.println(pf); Field f =…
Alexandr
  • 9,213
  • 12
  • 62
  • 102
55
votes
2 answers

Java final modifier

I was told that, I misunderstand effects of final. What are the effects of final keyword? Here is short overview of what I think, I know: Java final modifier (aka aggregation relation) primitive variables: can be set only once. (memory and…
Margus
  • 19,694
  • 14
  • 55
  • 103