• Post Reply Bookmark Topic Watch Topic
  • New Topic

Assigning to a final variable  RSS feed

 
femi Joseph
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello, please what is the result of the code below.


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, let's move this topic to the beginners Java forum. Second, what happen when you compiled it? What was the result?

Henry
 
femi Joseph
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:First, let's move this topic to the beginners Java forum. Second, what happen when you compiled it? What was the result?

Henry
   Nothing prints out. I read a chapter that says anytime you use a final variable you have to assign a value to it. So I think the code has a problem because the final x was not assigned. Am I correct?
 
Julian West
Ranch Hand
Posts: 91
3
Chrome Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nothing prints out because nothing in the code tells the program to print anything out.

Are you certain x isn't assigned a value? (x = ...)

Additionally, the post topic is "ternary operator" but the code doesn't have a ternary operator...may want to change that.
 
femi Joseph
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julian West wrote:Nothing prints out because nothing in the code tells the program to print anything out.

Are you certain x isn't assigned a value? (x = ...)

Additionally, the post topic is "ternary operator" but the code doesn't have a ternary operator...may want to change that.
  ok Thanks
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
femi femmy wrote:I read a chapter that says anytime you use a final variable you have to assign a value to it. So I think the code has a problem because the final x was not assigned. Am I correct?


Yes, you're correct. And no, you aren't. It's true that you have to assign a value to a final variable. However it's possible that the code fragment you posted might qualify as assigning a value to x in some cases... it's a bit more complicated than what you thought. It would be better to produce a compilable code fragment -- i.e. a whole class -- which includes that code somehow and see what the compiler says.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Julian West wrote:Additionally, the post topic is "ternary operator" but the code doesn't have a ternary operator...may want to change that.


I don't think Femi's allowed to edit the subject of the post, so I changed it to something which looks okay to me. Okay with you Femi, or should I change it to something else?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is actually possible to define a result in terms of the values of x and y after the code has been run. The if‑else can be used to define x, so you should be able to write...or something in a similar format. You should find it reasonably straightforward to calculate that sort of result
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:. . . you have to assign a value to a final variable. However it's possible that the code fragment you posted might qualify as assigning a value to x in some cases... . . .
That code snippet has two paths through it, via the if and via the else. The compiler is programmed to recognise that those two paths exhaust all possibilities, so it interprets the if‑else as meaning the code will definitely follow one path or the other and x will definitely have been assigned to by the end of the code shown. So I would expect it to compile all right.
It is not however programmed to be able to recognise this as exhausting all possibilities:-...so it cannot convince itself (even if the human reader can) that those two ifs cover all possibilities. You will probably get the same compiler error if you remove the final keyword because x is declared as a local variable (I think), and the same rule about having to be definitely assigned applies to local variables and final variables.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, if you don't try to use the variable, you won't get any error.This compiles fine and prints Hurray!

It gets even more interesting. If the compiler is certain the line that tries to read from the variable can not be reached, it won't complain.This compiles fine and prints Hurray!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!