[Effective Java Reading notes] Chapter III Class and interface 12th

Source: Internet
Author: User

12th minimizing the accessibility of classes and members

In a sense, we should try to limit the access of members to the smallest extent possible! For decoupling, development, testing, and optimization can be more independent.

For members (domains, methods, nested classes, and nested interfaces), there are four possible access levels, ranging from small to large:

1. Private, only the inside of the class that declares the member can access

2. Package-level private, any class within the package that declares the member's class can be accessed if the member does not display the declared access level, which is the level, so it becomes the default access level

3.protected, the class that declares the member and its subclasses can access, and any class in the package that declares the class can also access

4.public, any class can access

In addition, constants in Java are globally accessible, public static final integer values_integer = new Integer (10);

The Values_integer above is immutable, and the public static final array can be changed:

 Public classMaintestjava { Public Static FinalInteger Max_num =NewInteger (10);  Public Static FinalInteger[] Max_num_a = {NewInteger (10),NewInteger (10),NewInteger (10)}; /**     * @paramargs*/     Public Static voidMain (string[] args) {//TODO auto-generated Method StubInteger II =NewInteger (11); max_num_a[1] = II;//no errors, can be assignedMax_num = II;//There is an error, you cannot assign a value    }}        

So you can use the static final method of public to get the clone of the array, set the Max_num_a to private, and not change

[Effective Java Reading notes] Chapter III class and interface 12th

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.