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
0
votes
2 answers

Java Switch statement confusion

In my app (which is an Android game), I have a method which checks if the player is still alive, and if not, runs an animation and the game is over. There are currently, 3 different animations available depending on how the player loses. So, for…
Zippy
  • 3,826
  • 5
  • 43
  • 96
0
votes
2 answers

Could using final on local variables (NOT class declarations or class members) cause memory leak?

From a naive point of view, it looks like declaring a local variables (ie., variables inside a method) as "final" could cause memory leak. Eg., public static MyObject create() { final Long time = System.millis(); return new MyObject() …
One Two Three
  • 22,327
  • 24
  • 73
  • 114
0
votes
2 answers

BluetoothServerSocket - Why use a temporary variable?

What is the use of assigning temporary variables in the following code? What difference does it make if I use mmServerSocket directly. Why do I have initialise mmServerSocket as a final? private class AcceptThread extends Thread { private final…
0
votes
2 answers

Why is the final keyword used profusely inside of methods even though it's completely fine to not define them final

I asked a collegue why he used final keyword for the assignment in this method and his response was, "Why not?". I don't see the benefit of putting final here. Or often times, I see it in class definitions as well. protected void…
Horse Voice
  • 8,138
  • 15
  • 69
  • 120
0
votes
3 answers

Use of final keyword before oncreate to call txt file from assets (Android)

I want to read a TXT file from assets folder. But I have to do this on before onCreate (on main class). So it should be final. My code; public class MainActivity extends Activity { public static final String host = LoadDataTxt("host.txt"); …
kadir_cakir
  • 1,013
  • 1
  • 16
  • 22
0
votes
3 answers

Class constant final variable

This is my code. I need wordOfTheDay and answer to stay the same. I need a user to input an answer for "What is the word of the day" and "What is the answer to 3*8" and depending on their answer it will either be accepted as the correct answer or…
user4055424
0
votes
0 answers

How can I serialize an object which contains a finalized list as an attribute?

I have A class, which contains a list List. Both implements Serialization. The list type is final. I'm using both tpye in a GWT Project. A and B have to be serialized because of RTC communication. But this isn't working. I got a message that final…
Kroy
  • 299
  • 1
  • 5
  • 18
0
votes
5 answers

Java: startingPath as "public static final" exception

[Updated, sorry about the change but now to the real problem] I cannot include try-catch-loop there for the exception from the method getCanonicalPath(). I tried to solve the problem earlier with method and then declaring the value there. The…
hhh
  • 50,788
  • 62
  • 179
  • 282
0
votes
3 answers

Why was final not extended to apply to arrays and referenced objects?

While an array reference can be declared as final, the elements of the array cannot. Similarly, while an object reference may be declared as a final field, the object to which it refers may still be mutable. Why so?
Farhan stands with Palestine
  • 13,890
  • 13
  • 58
  • 105
0
votes
2 answers

Final String[] and Final String

I am confused. I tried using final String usr; and trying to change its value never worked, but when I used an array final String[] usr = {"", ""};, it worked. I even accessed it from this sgnup.addActionListener(new ActionListener(){ public…
rexendz
  • 3
  • 1
  • 1
  • 5
0
votes
1 answer

why use final as modifier when we have private in method classes

Let's consider that we have a method which has been declared as below: final private method myMethod() { } Why should we use final when we have private as a modifier. What exactly is the purpose of final here?
Alireza
  • 6,497
  • 13
  • 59
  • 132
0
votes
0 answers

PowerMock not ripping off java final class modifier

Trying to mock a final class using Mockito and PowerMock I'm getting the error : java.lang.IllegalArgumentException: Cannot subclass final class class MyFinalClass at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:447) at…
Bit-Man
  • 516
  • 4
  • 17
0
votes
4 answers

Can I make a File final if I want to write to it later?

Is it okay to make an object of type File, which I later would like to write into, have a final declaration? I'd like to pass it to a Runnable inner class.
user3475234
  • 1,503
  • 3
  • 22
  • 40
0
votes
3 answers

Initializing a final variable in an abstract class (Java)

So I have this abstract class public abstract class A { protected final boolean b; protected A (boolean b){ this.b = b; } } And this class that extends A public class C extends A{ protected C() { super(false); } } I…
Mechanic45
  • 173
  • 1
  • 6
  • 16
0
votes
3 answers

Different compilation error when final local variable is used with while loop

Sample code: Compilation error: The final local variable flag may already have been assigned final boolean flag; while (flag = false) { // I am using = instead of == just to test it System.out.println("inside loop"); } Compilation…
Braj
  • 46,415
  • 5
  • 60
  • 76