Alibaba--java specification

Source: Internet
Author: User
Tags shallow copy variable scope

18. "Recommended" final can declare classes, member variables, methods, and local variables, using the final keyword in the following cases:

1) classes that are not allowed to be inherited, such as: String.

2) do not allow modification of referenced domain objects, such as: The domain variables of the POJO class.

3) do not allow overridden methods, such as: The Setter method for the POJO class.

4) Local variables that are not allowed to be re-assigned during the run.

5) To avoid context reuse of a variable, use the final description to force a redefinition of a variable to facilitate better refactoring.

19. "Recommended" Use the Clone method of object to copy objects with caution. Said

Ming: The object's Clone method is a shallow copy by default, and if you want to implement a deep copy, you need to override the Clone method to implement a copy of the Property object.

20. "Recommended" class members and method access control are strictly:

1) If the external object is not allowed to be created directly from new, then the constructor must be private.

2) The tool class does not allow public or default construction methods.

3) class non-static member variable and shared with subclass, must be protected.

4) class non-static member variable and used only in this class, must be private.

5) class static member variable if used only in this class, it must be private.

6) If the static member variable, you must consider whether it is final.

7) The class member method is only intended for internal invocation of the class and must be private.

8) The class member method is only exposed to the inheriting class, so it is limited to protected.

Description: Any class, method, parameter, variable, strict access to the scope. Too broad a range of access, not conducive to module decoupling. Think: If it is a private method, want to delete the deletion, but a public service method, or a public member variable, delete, not palms sweat it? Variables like their own children, as far as possible within their own sight, variable scope is too large, unrestricted running around, then you will worry.

4. "Force" the method of using the collection to the array, must use the collection of ToArray (t[] array), passed in an array of exactly the same type, the size is list.size ().

Note: Using the ToArray parameter method, the ToArray method internally allocates the memory space and returns the new array address when the array space is not large enough for the incoming parameter assignment;

If the array element is larger than is actually required, the array element labeled [List.size ()] is set to NULL, the other array elements remain the original value, so it is best to align the method into the parameter group size definition with the number of collection elements.

Positive example:

List List = new ArrayList (2);

List.add ("Guan");

List.add ("Bao");

string[] Array = new string[list.size ()];

Array = List.toarray (array);

Counter Example:

There is a problem with using the ToArray method directly, the return value of this method can only be object[] class, if the strong to other types of array will appear classcastexception error.

5. When you use the tool class Arrays.aslist () to convert an array to a collection, you cannot use it to modify a collection-related method, and its Add/remove/clear method throws a Unsupportedoperationexception exception.

Description: The returned object of Aslist is a Arrays inner class and does not implement a modified method of the collection. Arrays.aslist is the adapter mode, just the conversion interface, the background data is still an array.

string[] str = new string[] {"You", "WU"};

List List = Arrays.aslist (str);

First case: List.add ("Yangguanbao"); Run-time exception.

Second case: str[0] = "Gujin"; Then the List.get (0) will be modified as well.

Alibaba--java 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.