Effective Java English Second Edition reading notes Item 13:minimize the accessibility of classes and members

Source: Internet
Author: User
Tags modifier

The visible field of the access modifier

Private-the member is accessible only from the top-level class where it is declared.
Package-private-the member is accessible from any class in the package
where it is declared. Technically known as default access, this is the access level
You get if no access modifier is specified.
Protected-the member is accessible from subclasses of the class where it is
Declared (subject to a few restrictions [JLS, 6.6.2]) and from any class in the
Package where it is declared.
Public-the member is accessible from anywhere.

Instance fields should never is public, so classes and public mutable fields is not Thread-safe

It's wrong for a class
To has a public static final array field, or an accessor that returns such a
Field. If A class has such a field or accessor, clients'll be is able to modify the contents
of the array. This is a frequent source of security holes:
Potential security hole!
public static final thing[] VALUES = {...};

Beware of the fact that many IDEs generate accessors so return references to private
Array fields, resulting in exactly this problem. There is ways to fix the
Problem. You can make the public array private and add a public immutable list:

private static final thing[] Private_values = {...};
public static final thing[] values () {
return Private_values.clone ();
}

Effective Java English Second Edition reading notes Item 13:minimize the accessibility of classes and members

Related Article

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.