6

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.

newbie
  • 14,582
  • 31
  • 104
  • 146
  • If you're calling it from a JSP, the getter should be `getIsLifeTimeMember()`. Get is assumed in a JSP. – Makoto Nov 27 '12 at 05:11
  • Please don't upload image. Just paste the code as it is here. – Rohit Jain Nov 27 '12 at 05:12
  • I assume you mean that the code is `boolean isLifeTimeMember;` and not `boolean isLifeTimeMember()`. Eclipse does not generate getters and setters for methods! – Ted Hopp Nov 27 '12 at 05:13
  • @Makoto: The two generated methods should work fine from JSP as well (may be that the EL needs to be `x.lifeMember = true`, though, without the prefix). – Thilo Nov 27 '12 at 05:14

2 Answers2

17

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".

Tarun Kumar
  • 2,918
  • 3
  • 15
  • 17
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • would it affect my jsp when i call the – newbie Nov 27 '12 at 05:20
  • @newbie - It will probably cause problems. As described in [this thread](http://stackoverflow.com/questions/2945061/java-how-to-name-boolean-properties), it's better to avoid jsp property names that read like questions (`isLifeTimeMember`) and instead just use the property itself as the property name (`lifeTimeMember`). – Ted Hopp Nov 27 '12 at 05:27
  • @MukulGoel - Did you see my comment just above yours? – Ted Hopp Nov 27 '12 at 05:32
  • Yes i did see that, but your post still reflects the old amswer – Mukul Goel Nov 27 '12 at 05:33
  • @MukulGoel - no longer. The old answer was not inconsistent with the comment, by the way. But I clarified the point I was trying to make. – Ted Hopp Nov 27 '12 at 05:38
  • jsp is dead. is IS back. Eclipse should update accordingly. – java-addict301 Nov 19 '19 at 16:58
2

In case of boolean variable, eclipse prepends is to the variable name to form the getter name. I.e. If variable is boolean present; then the gemerated getter would be named isPresent();

Its not advisable to have an is in the variable name.

If the variable name is ispresent , on jsp you will lookup by variable name ispresent which in turn looks up for its getter, its an boolean so it assumes getter would be isispresemt(); which was not there as the getter setter generator in eclipse does not add an is in case that already existe in variable name.

thus an exception could not find the field ispresent is expected to be thrown

having an is in field name , can cause problems, avoid using them

Mukul Goel
  • 8,387
  • 6
  • 37
  • 77