Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean Getter and Setter using eclipse

Tags:

java

eclipse

I am wondering why eclipse produces the following getter and setter if i used the following boolean:

boolean isLifeTimeMember

Image

The getter should be isIsLifeTimeMember() and not isLifeTimeMember()

I think it affected calling the variable isLifeTimeMember in JSP. because it will look at JSP and map it to isIsLifeTimeMember() getter method.

Error will result because there is no isIsLifeTimeMember() method but the getter generated by eclipse is isLifeTimeMember()

Thank you.

like image 628
newbie Avatar asked Nov 27 '12 05:11

newbie


1 Answers

Eclipse name generation rules are that boolean getters should start with is. If the variable name already starts with is, then it thinks that no additional prefix is necessary.

Eclipse has a setting that controls the use of is for generated boolean getters. Open up Preferences and navigate to Java > Code Style. There you can uncheck the option "Use 'is' prefix for getters that return boolean". Eclipse-generated boolean getters will then start with "get", just like all the others.

Java has no problem, by the way, in having a field and a method with the same name.

However, having property names that start with "is" will probably cause problems with jsp. As described in this thread, it's better to avoid property names that read like questions (isLifeTimeMember) and instead just use the property itself as the property name (lifeTimeMember).

Code Example:

boolean lifeTimeMember;

public boolean isLifeTimeMember() {
   return lifeTimeMember;
}

public void setLifeTimeMember(boolean lifeTimeMember) {
   this.lifeTimeMember = lifeTimeMember;
}

And in JSP if you need to use this variable simply use variable name "lifeTimeMember".

like image 190
Ted Hopp Avatar answered Sep 19 '22 13:09

Ted Hopp