File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes this=null What will happen. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "this=null What will happen." Watch "this=null What will happen." New topic
Author

this=null What will happen.

Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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
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
I see no ambiguity.
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
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
Originally posted by Jeff Albertson:
I see no ambiguity.


In what sense ?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I agree with Jeff. What is the ambiguity?
faisal usmani
Ranch Hand

Joined: Jan 14, 2006
Posts: 139
I think , this should not be assigned 'null' . correct me if i am wrong


regards
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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
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
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Found the bug report: https://bugs.eclipse.org/bugs/show_bug.cgi?id=38143
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

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

Joined: Aug 16, 2005
Posts: 14111
    
  16

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 ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Ankur Sharma:
Actually It's compiling finely.


That's the bug. It shouldn't.
Riya Pant
Greenhorn

Joined: Feb 17, 2006
Posts: 28
<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"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: this=null What will happen.