Java Bean Getters/Setters

https://dertompson.com/2013/04/29/java-bean-getterssetters/

Many Java developers think they know everything about Java Beans and the correct getter/setter styles, but there are some hidden traps ;-)

Let’s do a little quiz!

How should the correct and getter/setter for a property with the following field look like?

private String name;

This is an easy one:

public String getName() {
  return name;
}

public void setName(String name) {
  this.name = name;
}

Notice that the first letter of the field was made uppercase.

Here is a different one:

private String URL;

The correct getter/setter methods would look like:

public String getURL() {
  return URL;
}

public void setURL(String URL) {
  this.URL = URL;
}

In this example the field was already uppercase so nothing was changed in the getter/setter.

So what’s about a field like this:

private String iMessageId;

This one is a little bit tricky – could you guess the correct getter/setter?

public String getiMessageId() {
  return iMessageId;
}

public void setiMessageId(String iMessageId) {
  this.iMessageId = iMessageId;
}

It is important that the case was not changed – like for the URL field. This happens when the second letter of the field name is already uppercase. The reason for this is the method java.beans.Introspector.decapitalize:

/**
 * Utility method to take a string and convert it to normal Java variable
 * name capitalization.  This normally means converting the first
 * character from upper case to lower case, but in the (unusual) special
 * case when there is more than one character and both the first and
 * second characters are upper case, we leave it alone.
 * 

* Thus "FooBah" becomes "fooBah" and "X" becomes "x", but "URL" stays * as "URL". * * @param name The string to be decapitalized. * @return The decapitalized version of the string. */ public static String decapitalize(String name) { if (name == null || name.length() == 0) { return name; } if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) && Character.isUpperCase(name.charAt(0))){ return name; } char chars[] = name.toCharArray(); chars[0] = Character.toLowerCase(chars[0]); return new String(chars); }

I guess this one was new to some of you, wasn’t it?

The last example applies to boolean properties. We know that the getter/setter for

private boolean active;

may look like

public boolean isActive() {
  return active;
}

public void setActive(boolean active) {
  this.active = active;
}

or

public boolean getActive() {
  return active;
}

public void setActive(boolean active) {
  this.active = active;
}

But not everybody knows that the getter/setter for a property of the type Boolean:

private Boolean closed;

must look like

public Boolean getClosed() {
  return closed;
}

public void setClosed(Boolean closed) {
  this.closed = closed;
}

the getter isClosed() would not be recognized.

Not to long ago even Eclipse code generators made mistakes with those edge cases – I checked today and both IntelliJ and Eclipse generated everything correct. If you want to look into the specification you can do this here.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值