6

What should we expect from the following name? : mGage Program

if I camelCase this it will be mGageProgram and if I generate (in eclipse) the getters and setters I will get the following:

public String getmGageProgram() {
    return mGageProgram;
}

public void setmGageProgram(String mGageProgram) {
    this.mGageProgram = mGageProgram;
}

Which to me doesn't seem right as I was expecting the getMGageProgram() and setMGageProgram(value).

Are these getters/setters names alright?

Garis M Suero
  • 7,974
  • 7
  • 45
  • 68

3 Answers3

4

According to 8.8: Capitalization of inferred names of the JavaBeans API specification the names generated from the IDE are correct.

Alboz
  • 1,833
  • 20
  • 29
3

they are 100% correct :) but conventions differ among programmers , for me its method names in camel casing not variables. as for syntax its correct :)

  • Thanks. I was more concerned on serialization/deserialization object mappers than just a simple convention. It seems to work with Jackson and SpringMVC though. – Garis M Suero Oct 06 '14 at 21:27
1

I’d like to provide just a little more depth on what the spec says. It specifies how we get from the name of a getter and/or a setter to a property name. The interesting quote in this context is:

… to support the occasional use of all upper-case names, we check if the first two characters of the name are both upper case and if so leave it alone.

It’s from section 8.8: Capitalization of inferred names.

One example given is that URL (as in getURL or setURL) becomes (or stays) URL (not uRL).

So the method names that you and I would have expected, getMGageProgram and setMGageProgram, would have implied a property named MGageProgram with an upper case M. Since we wanted mGageProgram we need to use lower case m in the names of the getter and the setter.

The rules as I read them thus really allow you to use a lowercase letter right after get or set in any getter or setter name. This came as a peculiar surprise to me. Of course it’s not an option that we want to exploit in cases where we don’t have to.

Link: JavaBeans Spec download page

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161