Bug 399424

Summary: [quick assist] Move field initializer to constructor
Product: [Eclipse Project] JDT Reporter: RĂ¼diger Herrmann <ruediger.herrmann>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: ASSIGNED --- QA Contact:
Severity: enhancement    
Priority: P3 CC: markus.kell.r, ruediger.herrmann
Version: 4.3   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:

Description RĂ¼diger Herrmann CLA 2013-01-29 13:49:49 EST
There should be a quick assist on field declaration to move the initialization (if any) into the constructor(s)
For example:
  class Foo {
    int bar = 1;
  }
Ctrl+1 on 'bar' would offer to 'Move initializer to constructor' which would change the code to:
  class Foo {
    int bar;
    Foo() {
      bar = 1;
    }
  }
Comment 1 Markus Keller CLA 2013-01-30 12:30:21 EST
Moving the initializer is not 100% behavior-preserving in general, because field initializers are executed in declaration order, before the rest of the constructor is executed.

To minimize impact, the initialization should be inserted as the first possible statement in all constructors. E.g.:

public class Foo {
    int bar = 1;
    Foo() {
        super();
        bar++;
    }
}

... becomes:

public class Foo {
    int bar;
    Foo() {
        super();
        bar = 1;
        bar++;
    }
}