Community
Participate
Working Groups
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; } }
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++; } }