Java Bean Getters/Setters

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.

Deutsche Telekom erdrosselt Internet

Wenn ich so lese was die Deutsche Telekom mit ihren beschränkten neuen Tarifen so plant wird mir ganz heiß. Da fällt es mir schwer die richtigen Worte zu finden. Es zeigt einfach wieder, dass sich beim Thema “Internetanbindung” alles in die falsche Richtung entwickelt – die Anbindungen werden eher schlechter als besser. Ich habe ja hier schon des öfteren darüber geschrieben in welcher Situation ich mich selbst hier in Österreich befinde, aber zumindest kann ich mit meiner zur Verfügung stehenden Bandbreite noch machen was ich will.

http://drossl.de/ zeigt sehr schön was die Auswirkung dieses Vorhabens ist – wenn ich in ca. 7 Stunden schon mein ganzes Datenvolumen ausschöpfen kann und dann ein funktional kaputtes Netz habe dann ist so eine Internetanbindung wertlos.

Es geht um Netzneutralität! Ein Internetserviceprovider sollte eine dumme Röhre mit einem gewissen Durchmesser zur Verfügung stellen und den Inhalt der durchläuft nicht unterschiedlich nach der Art bewerten – andernfalls entstehen solche Dinge wie dass ein Provider Skype verbietet oder Torrent Downloads blockiert. Die Niederlande machen es vor wie es geht: Netzneutralität im Gesetz verankern!

 

A fresh start

Today I deleted my blog – not intentionally, it happened. I tried to upgrade MySQL from 5.1 to 5.5, it did not work as expected. I tried varios things and somewhere in between I issued the wrong rm command. I deleted both the backup directory and the tgz of the mysql database directory I made before the update.

After a short heat flash I thought: ok, know I have to import the database again from the SQL dump I have on my Amazon S3. A script copies it there every night. After extracting the tgz from S3 I was shocked that the sql file had a zero byte file size. It turned out that the backup script used an old password for the mysqldump command so it was not able to backup for over a year and I do not have any older backup file.

I am starting to accept that I lost most of the blog content I created since June 2005 (!!) though I can not fully accept it at the moment. There is something in me that thinks that there has to be another backup somewhere …

Some of the content could be restored from the web archive or Google cache and I think I will do that within the next weeks.

Seems like this is another hard lessons to learn, more backups in the future, better checks of those backups to make sure they work and being more careful with the rm command.