Bug 399424 - [quick assist] Move field initializer to constructor
Summary: [quick assist] Move field initializer to constructor
Status: ASSIGNED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 4.3   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: JDT-UI-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-01-29 13:49 EST by Rüdiger Herrmann CLA
Modified: 2013-01-30 12:30 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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++;
    }
}