Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
704 views
in Technique[技术] by (71.8m points)

java - Javabean convention - method naming for property gId

If I have a property 'gId' in my Java class what should the accessor method be named as?

getGId is what I assume.

If there were a property gURL I think it would be getGURL, which kind of looks ugly (not referring to the alternative spelling of girl though).

If the property was just url the method name getUrl is good on the eye and yeah I would not name the property as URL in the first place which would make the accessor ugly again - getURL

I remember reading from the Javabean Specification PDF somewhere about capitalization of properties and also cases involving acronyms but cant find it in the PDF anymore.

Does anyone have a reference to it or please confirm if I am right in what I am saying?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The reference you are interested in can be found in the Beans specification at Section 8.8.

That being said, it does not explicitly cover your particular case of gId/gURL. The specification says that to provide a getter/setter, we simply capitalize the first letter. To recover the property from the getter/setter, if the first two letters are uppercase, then the whole property is left as-is. Otherwise we decapitalize the first letter. So your getter would become getGURL, but your property would be incorrectly recovered from the getter as GURL. You have the same problem with gId.

Therefore it seems that the specification does not allow you to provide a consistent translation of any property with a first lowercase character followed by an uppercase character.

My suggestion is to either adopt a completely lowercase property, or to extend the lowercase prefix to two letters (glURL, for example).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...