The rule of thumb is simple: make each class or member as inaccessible as
possible. In other words, use the lowest possible access level consistent with the
proper functioning of the software that you are writing.
For top-level (non-nested) classes and interfaces, there are only two possible
access levels: package-private and public. If you declare a top-level class or interface
with the public modifier, it will be public; otherwise, it will be package-private.
If a top-level class or interface can be made package-private, it should be. By
making it package-private, you make it part of the implementation rather than the
exported API, and you can modify it, replace it, or eliminate it in a subsequent
release without fear of harming existing clients. If you make it public, you are
obligated to support it forever to maintain compatibility.
If a package-private top-level class (or interface) is used by only one class,
consider making the top-level class a private nested class of the sole class that uses
it (Item 22). This reduces its accessibility from all the classes in its package to the
one class that uses it. But it is far more important to reduce the accessibility of a
gratuitously public class than of a package-private top-level class: the public class
is part of the package’s API, while the package-private top-level class is already
part of its implementation.
For members (fields, methods, nested classes, and nested interfaces), there are
four possible access levels, listed here in order of increasing accessibility:
• 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.
After carefully designing your class’s public API, your reflex should be to
make all other members private. Only if another class in the same package really
needs to access a member should you remove the private modifier, making the
member package-private. If you find yourself doing this often, you should reexamine
the design of your system to see if another decomposition might yield
classes that are better decoupled from one another. That said, both private and
package-private members are part of a class’s implementation and do not normally
impact its exported API. These fields can, however, “leak” into the exported API
if the class implements Serializable (Item 74, Item 75).
For members of public classes, a huge increase in accessibility occurs when
the access level goes from package-private to protected. A protected member is
part of the class’s exported API and must be supported forever. Also, a protected
member of an exported class represents a public commitment to an implementation
detail (Item 17). The need for protected members should be relatively rare.
分享到:
相关推荐
Item 15: Minimize the accessibility of classes and members Item 16: In public classes, use accessor methods, not public fields Item 17: Minimize mutability Item 18: Favor composition over inheritance ...
of the different processes, and then choose the appropriate method to minimize the effects of voltage dips, within the constraints of the supply and budget. The worst case scenario is that of a three ...
bumpless transfer (AWBT) compensation to minimize the adverse eflects of any control input nonlinearities on closed loop performance". The resulting AWBT compensation is applicable to multivariable ...
This book addresses the topic of software design: how to decompose complex software systems into modules (such as classes and methods) that can be implemented relatively independently. The book first ...
Develop the mathematical foundation needed to help minimize the impact of the problem using easy-to-understand language and examples Use diverse tools and techniques such as machine learning ...
Maintenance Model of Power System with Matlab and Yalmip, including the maintenance of branches and generators, and a partial mantenance is allowed for branches. The Object Functions are 1.Maximize ...
Maintenance Model of Power System with Matlab and Yalmip, including the maintenance of branches and generators, and a partial mantenance is allowed for branches. The Object Functions are 1.Maximize ...
At present, the main ... they also cause extra errors on account of the effects of the ionosphere temporal and spatial gradients. Based on the analysis of these errors as well as the smoothing noise
Because of the expenses it is the interest of the factory as well as of the customer to minimize the number of parcels necessary to deliver the ordered products from the factory to the customer....
The integration of project information resources is crucial to centralize data and improve accessibility. By consolidating scattered resources into a unified platform, P company can streamline ...
If a model is linear in the parameters, estimation is based on methods from linear algebra that minimize the norm of a residual vector. If a model is nonlinear in the parameters, estimation is based ...
In the past automation of the power network was a very specialized area but recently due to deregulation and privatization the area has become of a great importance because companies require more ...
to speed the process of entering data and to minimize the possibility of errors in the EDS. Freeware based on: CIP Specification Edition 3.23 EtherNet/IP Adaptation of CIP Edition 1.23, Device...
blocks for constructing descriptors which can be combined together and jointly optimized so as to minimize the error of a nearestneighbor classifier. We consider both linear and nonlinear transforms ...
IB posts RDS_FRAG_SIZE fragments of pages to the receive queues to try and minimize the amount of memory tied up both the device and socket receive queues.
Autonomous navigation is an ...suited to make decisions about how to navigate in order to minimize the uncertainty of the joint belief about the robot's position and the state of the environment.
This paper describes the general architecture of the interface, its current shortcomings and advantages, and presents a set of modeling results for short range acoustic propagation, which accounts ...
architectures of fault data to minimize the loss of information. Experiment results show that the proposed method not only improves the divisibility between faults and normal process, but also ...