Effective Java study notes -13

Members of the class and accessibility minimized

Good design module, other modules for external and internal data hiding implementation details, and the API for clear isolate.
The advantage is that you can effectively decouple the relationship module, the module can be independent of the development, testing, optimization, speed up development.

  • As far as possible so that each class member or not be accessed outside world.
    Private (private): Only in a statement inside the top of the class members can access the member.
    Packet-level private (package-private): statement inside the package member of any class can access the member. This is also known as the "default (default) access levels," If you do not specify an access modifier is a member, on the use of this access level.
    Protected (protected): allows sub-classes and where the package is allowed to access.
    Publicly owned (public): can be accessed anywhere.
  • If the class implements Serializable interface, then these fields is likely to be leaked to the export of the API
  • If the method covers a method in a superclass, then the subclass level of access permitted to fall below the level of access in the superclass
  • Examples of the public domain is not. The same advice applies to a static field. With one exception, public static final field exposure constant practice constitutes underlined capital letters +

Nested class
static nested classes: non-static member can not be accessed outside the class, rarely used
non-static nested classes (inner classes), and then can be divided into the following three categories. Inner class is still a separate class, inner classes after compilation will be compiled into a separate .class file, but preceded by the $ symbol classes and life outside the class. Inner class can not access the ordinary way. Inner class is a member of the outer class, so the internal class member variables can be freely accessed outside the class, regardless of whether it is private.
A class defined directly
a method defined
anonymous inner classes

Original: Big Box  Effective Java study notes -13


Guess you like

Origin www.cnblogs.com/petewell/p/11614909.html