This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this=null What will happen.

 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 502
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My compiler gives an error while compiling that line. It says "cannot assign a value to final variable this"
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see no ambiguity.
 
faisal usmani
Ranch Hand
Posts: 139
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 139
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albertson:
I see no ambiguity.


In what sense ?
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Jeff. What is the ambiguity?
 
faisal usmani
Ranch Hand
Posts: 139
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think , this should not be assigned 'null' . correct me if i am wrong


regards
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found the bug report: https://bugs.eclipse.org/bugs/show_bug.cgi?id=38143
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually It's compiling finely

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 ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ankur Sharma:
Actually It's compiling finely.


That's the bug. It shouldn't.
 
Riya Pant
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<br>I tried this on WSAD which is running java 1.4.<br>Compiler is giving error "The left-hand side of assignment must be a variable"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic