Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getters/setters when first word has second letter capitalized

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?

like image 327
Garis M Suero Avatar asked Oct 06 '14 21:10

Garis M Suero


People also ask

Should setters be capitalized?

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.

What can I use instead of getters and setters?

You may use lombok - to manually avoid getter and setter method. But it create by itself. The using of lombok significantly reduces a lot number of code.

Can getter and setter have same name?

“Another thing to keep in mind when using getter (and setter) methods is that properties cannot share the same name as the getter/setter function.”

What's the advantage of using getters and setters that only get and set instead of simply using public fields for those variables?

The getter and setter method gives you centralized control of how a certain field is initialized and provided to the client, which makes it much easier to verify and debug. To see which thread is accessing and what values are going out, you can easily place breakpoints or a print statement.


3 Answers

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

like image 103
Alboz Avatar answered Oct 26 '22 19:10

Alboz


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

like image 45
Jean Raymond Daher Avatar answered Oct 26 '22 18:10

Jean Raymond Daher


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

like image 1
Ole V.V. Avatar answered Oct 26 '22 19:10

Ole V.V.