Automatically add private qualifier to fields in eclipse
Is there a way to automatically add the private qualifier while new variables are declared in Eclipse?
In a way I would like to override the default access to private
I don't know of a way to do this.
However, the way i write code, it would rarely be necessary. That's because i rarely define fields by hand; instead, i let Eclipse create them, and when it does that, it makes them private.
Say i want to create a class Foo
with a single field bar
of type int
. Start with:
public class Foo {
}
Put the cursor in the class body, hit control-space, and choose 'default constructor' from the proposals menu. You now have:
public class Foo {
public Foo() {
// TODO Auto-generated constructor stub
}
}
Delete the helpful comment. Now manually add a constructor parameter for bar
:
public class Foo {
public Foo(int bar) {
}
}
Now put the cursor on the declaration of bar
and hit control-1. From the proposals menu, choose '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 field.
There is a similar sequence of automatic operations which can create a field from an existing expression in a method (first creating a local variable, then promoting it to a field).
If you consider it more important to you than performance and readability, I suppose you could configure a relatively convenient solution as follows. I wouldn't do this myself.
For class and instance variables, modify the class template in preferences to incorporate this:
private static Object fields = new Object () {
// declare all class variables here
};
private Object vars = new Object () {
// declare all instance variables here
};
For local variables, modify the method template in preferences to incorporate this:
private Object locals = new Object () {
// declare all local variables here
};
Class variable x
will be declared in fields
. It will be private at this.class.fields.x
.
Instance variable y
will be declared in vars
. It will be private at this.vars.y
.
Local variable z
will be declared in locals
. It will be private at locals.z
.
If you do this, you can expect your entire program to be slower and use more memory that it would otherwise.
链接地址: http://www.djcxy.com/p/15122.html