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?
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.
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.
“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.”
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.
According to 8.8: Capitalization of inferred names of the JavaBeans API specification
the names generated from the IDE are correct.
they are 100% correct :) but conventions differ among programmers , for me its method names in camel casing not variables. as for syntax its correct :)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With