Isn't it ambiguious solution this program is giving could anyone explain me what is happening here in this code. Attached is the code.
Jayesh Lalwani
Ranch Hand
Joined: Nov 05, 2004
Posts: 502
posted
0
My compiler gives an error while compiling that line. It says "cannot assign a value to final variable this"
Jeff Albertson
Ranch Hand
Joined: Sep 16, 2005
Posts: 1780
posted
0
I see no ambiguity.
faisal usmani
Ranch Hand
Joined: Jan 14, 2006
Posts: 139
posted
0
Originally posted by Jayesh Lalwani: My compiler gives an error while compiling that line. It says "cannot assign a value to final variable this"
Mine too!!!
faisal usmani
Ranch Hand
Joined: Jan 14, 2006
Posts: 139
posted
0
Originally posted by Jeff Albertson: I see no ambiguity.
In what sense ?
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
posted
0
I agree with Jeff. What is the ambiguity?
faisal usmani
Ranch Hand
Joined: Jan 14, 2006
Posts: 139
posted
0
I think , this should not be assigned 'null' . correct me if i am wrong
regards
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
posted
0
No it should not be assigned to null. Based on the message the compiler gives, this is a final variable associated with the class and like other final variables, it cannot be assigned a new value. But that isn't ambiguous.
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
posted
0
If I remember correctly, an early version of the Eclipse compiler didn't flag assignment to "this" as an error. I don't remember whether that was a bug, or something that actually officially changed with later Java versions...
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Actually It's compiling finely. But I don't understand if we are assigning current object to null then how it's going to System.out.println() is working. Please let me clearify.
On which Java version? Did you see what the people above are saying about Eclipse? Are you using Eclipse? Eclipse has its own built-in Java compiler. According to the bug report above, there's a bug in Eclipse that allows you to assign a value to "this". It shouldn't be possible.
It's not interesting to find out why it still works - it's an illegal Java program and the behaviour is undefined. You noticed that the System.out.println() still works, but it just as well could not have worked. What the program does, is undefined. It could do anything. It could even make your hair turn purple. [ April 06, 2006: Message edited by: Jesper de Jong ]