• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

this=null What will happen.

 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My compiler gives an error while compiling that line. It says "cannot assign a value to final variable this"
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see no ambiguity.
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeff Albertson:
I see no ambiguity.



In what sense ?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Jeff. What is the ambiguity?
 
faisal usmani
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Found the bug report: https://bugs.eclipse.org/bugs/show_bug.cgi?id=38143
 
Shaan Shar
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ankur Sharma:
Actually It's compiling finely.



That's the bug. It shouldn't.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic