I do not know how to do that.
However, as I write the code, this is rarely necessary. This is because I rarely define fields manually; instead, I let Eclipse create them, and when it does, it makes them private.
Let's say I want to create a Foo class with one bar field of type int . Start with:
public class Foo { }
Place the cursor in the body of the class, press control-space and select "default constructor" from the suggestions menu. Now you have:
public class Foo { public Foo() {
Delete useful comment. Now manually add the constructor parameter for bar :
public class Foo { public Foo(int bar) { } }
Now place the cursor on the bar declaration and press control-1. In the offers menu, select "Assign Parameter to New Field":
public class Foo { private final int bar; public Foo(int bar) { this.bar = bar; } }
Bingo. Now you have a personal field.
There is a similar sequence of automatic operations that can create a field from an existing expression in a method (first creating a local variable and then moving it into the field).
Tom anderson
source share