15

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.

5
  • What is your question? Commented May 15, 2015 at 20:27
  • "better" - better how? What criteria will you use for judging an answer? Commented May 16, 2015 at 1:31
  • Also, it is a poor practice to initialize variables to meaningless values. Compilers are really good at finding code paths where you've forgotten to set a meaningful value. If you initialize it, the compiler can't help you as much. And, humans are not good at discerning that you didn't mean anything when you initialized the variable.(A really good compiler/IDE would tell you that you never used the value you set on line 1.) Commented May 16, 2015 at 2:44
  • I agree. That is why I asked my question. Commented May 16, 2015 at 18:41
  • "Is there a better way to do this?" Yes, create a method. Something like final String name = getName();. The advantage is, that the method (name) can make it clearer where a value comes from or what that value means. You can still use the version of @rgettman, but don't forget my suggestion :D. Commented May 16, 2015 at 19:14

2 Answers 2

26

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.";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Now, and I swear this is what happened, I first tried it exactly like that and Eclipse gave me an error with the assignment in the else clause. But I just tried it again now and it works without giving me a compile error. So, obviously I didn't enter it correctly the first time.
@VingInMedina There is a decent chance that Eclipse messed up or was behind. Eclipse is.. not great.
14

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.";

3 Comments

In some cases, the use of the ternary operator is discouraged because it confuses the readability of code. Moreover, a compiler could optimize an if statement into a ternary operator (or viceversa) if needed
@RaulLuna In this case it's much more readable and concise: 1 line instead of 6. Performance-wise the difference is negligible if any...
@RaulLuna for selecting one value from two for a variable, a ternary operator is far more readable than an if (shorter, only one assignment, only one scope). Anyone who doesn't immediately understand this piece of code simply doesn't know Java; don't pander to them by writing ugly code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.