Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java use of non-final variable to set final variable

This is probably a dumb question, but I'll risk asking it anyway.

Often I need to create a final variable for use somewhere else and the value of that variable will need to be set based on some condition. This is how I typically do it:

String notFinalVersion = null;
if (someThing == 1) {
    notFinalVersion = "The value was 1.";
} else {
    notFinalVersion = "The value is not 1.";
}
final String finalVersion = notFinalVersion;

I can then use the variable finalVersion where I need to. But, this just seems somehow wrong. Is there a better way to do this?

Edit: By "better", I meant that I was looking for a method for defining the final variable that did not require that I create an extra variable. I knew that creating an extra variable was inefficient and not a good practice, and I was positive that there must be a way of doing what needed to be done without the extra steps.

I received an answer, which I have marked as accepted. As I stated in my comment, I had originally tried the solution provided, but received an error from Eclipse. I must have either typed it incorrectly the first time, or Eclipse has something of a "hiccup".

I accept that there are many ways of doing something and that what one person will accept as the best way is not what someone else would consider. However, all of the answers included here, were clear and to the point and, I feel, solved my problem.

like image 558
VingInMedina Avatar asked May 15 '15 20:05

VingInMedina


People also ask

Can we assign final variable in Java?

In Java, non-static final variables can be assigned a value either in constructor or with the declaration. But, static final variables cannot be assigned value in constructor; they must be assigned a value with their declaration.

Can we use Non-final variable in lambda?

A non-final local variable or method parameter whose value is never changed after initialization is known as effectively final. It's very useful in the context of the lambda expression. If you remember, prior to Java 8, we cannot use a non-final local variable in an anonymous class.

Can you assign a final variable?

A final variable can be explicitly initialized only once. A reference variable declared final can never be reassigned to refer to an different object. However, the data within the object can be changed. So, the state of the object can be changed but not the reference.

What is non-final variable?

So, it is made possible in program as the variables are declared as non-final. So, the value of these variables can be changed in the program. JVM thinks that at any point in time there is a chance that the change the value of a and b, and the last statement may be executed, so no error is coming in the second program.


2 Answers

You can declare a final variable and not assign it yet, as long as it is definitely assigned before you use it. Don't assign it null to begin with, else it will be definitely assigned already. Assign it as you are already doing with your if/else blocks, and you'll be fine.

final String finalVersion;
if (someThing == 1) {
    finalVersion = "The value was 1.";
} else {
    finalVersion = "The value is not 1.";
}
like image 59
rgettman Avatar answered Sep 19 '22 12:09

rgettman


In addition to @rgettman's answer, for this case you can just use the ternary operator:

final String finalVersion = someThing == 1 ? "The value was 1." : "The value is not 1.";
like image 42
M A Avatar answered Sep 19 '22 12:09

M A