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

avoiding "Potential null pointer access"

which would you say is the best practice when implementing the following problem: MyClass myVariable = null; if ( ..condition 1.. ) { myVariable = new MyClass(1); } else if ( ..condition 2.. ) { myVariable = new…
user1414745
  • 1,317
  • 6
  • 25
  • 45
0
votes
4 answers

final int in Java

I have a relatively simple question regarding the function of final in Java. When I compile this code: import java.util.*; import java.lang.*; import java.io.*; class Arrays { public static void main (String[] args) throws java.lang.Exception …
Javasaurus
  • 19
  • 1
  • 1
  • 4
0
votes
2 answers

A final class and underlying consequences in Java

I heard this line somewhere and I cannot get it off of my mind: "All members of a final class are implicitly final." Now, I know very well these three famous concepts: A final class cannot be extended. A final variable cannot be re-assigned with a…
0
votes
6 answers

Immutable Java class with non-final member

I still have some problems grasping the idea of immutability in Java. I understand that it differs from the const-ness in C++ and that a final class that only has final members of classes that are immutable themselves is immutable. E.g. the…
Manuel
  • 6,461
  • 7
  • 40
  • 54
0
votes
1 answer

How can non-final variables be switch constants?

public class SwitchTest { public static void main(String[] args) { Integer i = new Integer(2) + new Integer(2); switch(i){ case 4: System.out.println("foo"); break; default: System.out.println("default"); break; } …
Codistan
  • 1,469
  • 1
  • 13
  • 17
0
votes
3 answers

Why is this an endless loop?

First of all sorry for this, I really think it is a silly question but I've been stuck on this for a while. So maybe you can help me. The problem is I don't truly understand what's wrong in the code. So let's look up. void enter() { int init= 1,…
blackout
  • 84
  • 9
0
votes
2 answers

Powermock/EasyMock: Set expectation on final method that would throw exception

How do I set an expectation on a final method if I can't safely invoke that method at all? PowerMock is supposed to ensure the invocation is mocked, but I can't even get to that stage: WithFinal.java: public class WithFinal { public final void…
lmm
  • 17,386
  • 3
  • 26
  • 37
0
votes
2 answers

assign the final variable with the function return type

In java i tried to assign the final variable with the function return value. But i got the compilation errors. Can anyone help me what is causing the errors here? the code is like this: public class A{ private static final set set1 =…
0
votes
2 answers

how to change final value of StringProperty type in javaFX?

i was trying to learn Tableview and got some example. i dont get how StringProperty works. Although class Person's fields are final instanace, setEmailButton can change its value. import javafx.application.Application; import…
흠좀무
  • 1
  • 1
  • 2
0
votes
4 answers

How to use a string id from res/values/strings.xml instead of String or CharSequence?

If I have this piece of code with hardcoded String "New event of importance": public class MainActivity extends Activity { private NotificationManager mNManager; private static final int NOTIFY_ID = 1100; @Override public void…
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
0
votes
1 answer

Local variable is accessed from within inner class

I have check out answers like thees Variable is accessed within inner class. Needs to be declared final but it does not address what I am after I have an on click listener and here is the code mUsername is a EditText that is already defined same…
iqueqiorio
  • 1,149
  • 2
  • 35
  • 78
0
votes
1 answer

Should members of static class in Builder pattern be final?

So I have seen a couple of examples on Builder Pattern. In all, The static class or Builder only has those memebers as final that can be intitialized by the constructor. Example - public class User { private final String firstName; //…
Vivin
  • 1,327
  • 2
  • 9
  • 28
0
votes
1 answer

Add dynamic data to onClickListener of TextViews in Table Layout

I have made dynamic table rows with two TextViews in each row with a different id (local variable in Java file - see code). I want to open another activity with an image with name say using the same id of the TextView.(which i can send using…
Himanshu Aggarwal
  • 1,803
  • 2
  • 24
  • 36
0
votes
1 answer

Modifying an object's attributes within an inner class

I have an implementation of ArrayAdapter here that's responsible for populating a listview which displaying multiple views, among which a seekbar. Right now, I'm trying to implement a OnSeekBarChangeListener for these seekbars. I have a TextView…
Anubis
  • 1,162
  • 4
  • 14
  • 31
0
votes
1 answer

Why was 'effectively final' introduced in Java 8?

The difference of a local variable being final or effectively final has been discussed here. I do not really understand though, why it was introduced in Java 8. To me it seems like it just gives the programmer the freedom to leave out the final…
Mathias Bader
  • 3,585
  • 7
  • 39
  • 61