In-depth understanding of attribute names and Setter/getter methods in the JavaBean specification

Source: Internet
Author: User

JavaBean Specification Document: http://download.oracle.com/otndocs/jcp/7224-javabeans-1.01-fr-spec-oth-JSpec/

About the Property name section:

Utility method to take a string and convert it to normal Java variable name capitalization. This normally means converting the first character from upper case to lower case, but in the (unusual) special case when t Here's more than one character and both the first and second characters be upper case, we leave it alone.
Thus "Foobah" becomes "Foobah" and "X" becomes "X", but "url" stays as "url".

Tool translation + Personal modification:

The tool method puts a string and converts it to uppercase in the normal Java variable name. This usually means that the first character is converted from uppercase to lowercase,
However, in special cases where the current two characters or multiple characters are uppercase, we do not process him (return directly).

Therefore, "Foobah" becomes "Foobah" and "X" become "X", but "url" is still "url".

The above content is actually a java.bean.Introspector#decapitalize Javadoc comment for this method.

Here's how:

/** * Utility method to take a string and convert it to normal Java variable  * Name capitalization. This normally means converting the first * character from upper case to lower case, but in the (unusual) special * case WH En there is more than one character and both the first and * second characters be upper case, we leave it alone. * <p> * Thus "Foobah" becomes "Foobah" and "X" becomes "X", but "url" stays * as "url". * * @param name the string to be decapitalized. * @return The decapitalized version of the string.    */public static string Decapitalize (string name) {if (name = = NULL | | name.length () = = 0) {return name; } if (Name.length () > 1 && character.isuppercase (Name.charat (1)) && Character.isu    Ppercase (Name.charat (0))) {return name;    } char chars[] = Name.tochararray ();    Chars[0] = Character.tolowercase (chars[0]); return new String (chars);} 

We use this method to test several different field names:

Look at the following code:

public class Testbean {public    static void Main (string[] args) {        System.out.println (introspector.decapitalize ( "Name"));        System.out.println (Introspector.decapitalize ("Name"));        System.out.println (Introspector.decapitalize ("Mname"));        System.out.println (Introspector.decapitalize ("NAME"));}    }

Output:

Name
Name
Mname
NAME

The result is the same as the results, that is, name Name if the field name is Name , it will be name used, so from this point of view the naming method is wrong and does not conform to the specification.

Mname is no problem. There is no problem with NAme, and name,name is not wrong.

This article says that depth may not be enough, and the setter and getter in the title are not mentioned at all. If you do not understand the place, welcome to leave a message!

In-depth understanding of attribute names and Setter/getter methods in the JavaBean specification

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.