Bug 46918

Summary: assignment to this allowed by compiler
Product: [Eclipse Project] JDT Reporter: Orjan Petersson <orjan.petersson>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: ijba, stephan.herrmann, y-tanaka
Version: 2.1.2   
Target Milestone: 2.1.3   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description Orjan Petersson CLA 2003-11-19 04:22:56 EST
The built-in compiler does not flag assignment to this as an error.
For example, the following class compiles and runs:

package assign2this;
public class Assign2This {
  Assign2This() {
    this = null; 
    System.out.println("this: " + this);
  }
  public static void main(String[] args) {
    Assign2This t = new Assign2This();
    System.out.println("ctor returned:" + t);
  } 
}

Compiling with the Sun 1.4.2_02 javac returns an error:
C:\orpe\jtst>c:\j2sdk1.4.2_02\bin\javac Assign2This.java
Assign2This.java:3: cannot assign a value to final variable this
        this = null;
        ^
1 error
Comment 1 Philipe Mulet CLA 2003-11-19 07:56:55 EST
In 3.0 recent build, an error is correctly reported against the assignment to 
this.
Comment 2 Philipe Mulet CLA 2003-11-19 10:42:10 EST
Reproduced in 2.1 stream.
Comment 3 Philipe Mulet CLA 2003-11-19 12:02:07 EST
Fixed in 2.1 stream (backported 3.0 code).
Comment 4 Philipe Mulet CLA 2003-11-19 12:18:11 EST
Added regression test: InitializationTest#test180.
Comment 5 David Audel CLA 2003-11-24 10:25:53 EST
Verified for 3.0M5
Comment 6 Philipe Mulet CLA 2004-01-27 12:14:40 EST
*** Bug 50688 has been marked as a duplicate of this bug. ***
Comment 7 David Audel CLA 2004-03-01 12:04:05 EST
Verified for 2.1.3 (M20040225)
Comment 8 Philipe Mulet CLA 2004-04-15 07:33:22 EDT
*** Bug 58521 has been marked as a duplicate of this bug. ***
Comment 9 Philipe Mulet CLA 2004-05-14 06:59:56 EDT
*** Bug 62205 has been marked as a duplicate of this bug. ***