Java – automatically adds private qualifiers to fields in eclipse
Is there any way to automatically add private qualifiers when declaring new variables in eclipse?
To some extent, I want to override the default access to private
Solution
I don't know how to do that
However, the way I write code is rarely necessary That's because I rarely define fields by hand; Instead, I let eclipse create them, and when it does, it makes them private
Suppose I want to create a class foo with a single field column of type int From
public class Foo { }
Place the cursor in the class body, click control space, and then select "default constructor" from the proposal menu You now have:
public class Foo { public Foo() { // TODO Auto-generated constructor stub } }
Delete useful comments Now manually add constructor parameters for bar:
public class Foo { public Foo(int bar) { } }
Now place the cursor over the bar declaration and click control-1 From the proposal menu, select assign parameter to new field:
public class Foo { private final int bar; public Foo(int bar) { this.bar = bar; } }
bingo. You now have a private sphere
There is a similar sequence of automatic operations that can create a field from an existing expression in a method (first create a local variable and then promote it to a field)