How can I tell (if ever) Eclipse to make a single line for a getter or setter when using auto formatting?
public User getUser() {
return user;
}
to:
public User getUser() { return user; }
Go to Source | Format Document or press Ctrl+Shift+F.
You can't make getter and setter methods static if you use any attributes or properties that aren't static.
If you don't like all the boilerplate which Java forces you to write, you might be interested in Project Lombok as an alternative solution.
Instead of trying to format your code to minimize the visual impact of getters and setters, Project Lombok allows them to be added by the compiler behind the scenes, guided by annotations on your class's fields.
Instead of writing a class like this:
public class GetterSetterExample {
private int age = 10;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
protected void setName(String name) {
this.name = name;
}
}
You would write:
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
public class GetterSetterExample {
@Getter @Setter private int age = 10;
@Setter(AccessLevel.PROTECTED) private String name;
}
(example from: http://projectlombok.org/features/GetterSetter.html)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With