Special case of JavaBean attribute naming rules

Source: Internet
Author: User

Yao Hong said in the last week that if the second letter of the JavaBean attribute value is capital, some eclipse may have problems. Under Ye Du's guidance, he studied the issue and summarized it as follows:

Generally, the JavaBean attribute starts with a lower-case letter and is named in the hump format. The corresponding getter/setter method is to connect get/set to the upper-case attribute name of the first letter. For example, the property name is username, and the corresponding getter/setter method is GetUserName/setusername.

However, there are some special cases:

1. If the second letter of the attribute name is in uppercase, the name is directly used as the last part of the get/set in the getter/setter method, that is, the case is not changed. For example, the property name is uname and the method is getuname/setuname.

2. If the first two letters are in upper case (generally, both nouns and acronyms are in upper case), the attribute name is directly used as the last part of the get/Set Method in the getter/setter method. For example, the property name is URL and the method is geturl/seturl.

3. If the first letter is in upper case, it is also the last part of the get/set attribute name in the getter/setter method. For example, the property name is "name" and the method is "getname/setname". This is the worst case. An error occurs because the default property name is "name.

Therefore, you must comply with the above naming rules when naming a JavaBean.

Note that the getter/setter method automatically generated by some development tools does not take into account the special cases mentioned above, resulting in the generation of bugs, such as eclipse3.4 and eclipse3.5 is more intelligent than 3.4, some problems are avoided. The following are my experiments in eclipse3.5 and eclipse3.4 respectively:

In the object class of the user, two variables are set: username and uname. Then, the get and set methods (source-> Generate getter andsetter) automatically generated by eclipse are used ), the correct method shown in eclipse3.5 is generated. The normal method is to change the first letter of the variable name to uppercase, and if the first letter of the variable name is lowercase, the second letter is in upper case, and the first letter is in lower case.

 

 

Then, inject bean to the user class in the spring configuration file.

 

Next, run a test method to output two variables.

 

The output result is as follows.

 

In eclipse3.4, such a naming method may lead to problems. Looking at the marked red, eclipse3.4 simply converts the first letter to uppercase.

 

In this way, in the same configuration file, a problem occurs when running the test method. The uname attribute cannot be found because it has become uname, therefore, the uname variable cannot be found during bean creation.

 

 

Uppercase letters:

 

Output result:

 

Uppercase letters:

 

 

The following error occurs:

 

I hope you will pay attention to special situations when naming attributes to avoid problems.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.