Instance members vs.. class members
I will describe the differences between instance members and class members with particular emphasis being placed on their accessibility.
Three kinds of objects
From a conceptual viewpoint, there are at least three kinds of objects involved in a Java program:
- Ordinary objects
- Array objects
- Class objects
All of the discussion up to this point in the miniseries deals with what I have referred to in the above list as ordinary objects.
These are the objects that you instantiate in you code by applying the new operator to a constructor for a class in order to create a new instance (object) of that class. (There are also a couple of other ways to create ordinary objects, but I'm not going to get into that at this time.)
Array objects
I haven't discussed array objects thus far in this miniseries. (I plan to discuss them in a subsequent lesson.)
Suffice it for now to say that Array objects are objects whose purpose is to encapsulate a one-dimensional array structure that can contain either primitive values, or references to other objects (including other Array objects).
I will discuss Class objects in this lesson.
Discussion and Sample Code
Let me emphasize at the beginning that the following discussion is conceptual in nature. In this discussion, I will describe how the Java system behaves, not how it is implemented. In other words, however it is implemented, it behaves as though it is implemented as described below.
The class named Class
There is a class whose name is Class. The purpose of this class is to encapsulate information about some other class (actually, it can also be used to encapsulate information about primitive types as well as class types).
Here is part of what Sun has to say about this class:
"Instances of the class Class represent classes and interfaces in a running Java application. ...What does this mean?Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded ..."
As a practical matter, when one or more objects are instantiated from a given class, an extra object of the Class class is also instantiated automatically. This object contains information about the class from which the objects were instantiated.
(Note that it is also possible to cause a Class object that describes a specific class to be created in the absence of objects of that class, but that is a topic that will be reserved for more advanced lessons.)A real-world analogy
Here is an attempt to describe a real-world analogy. Remember that a class definition contains the blueprint for objects instantiated from that class.
A certain large construction company is in the business of building condominium projects. This contractor builds condos of many different sizes, types, and price ranges. However, each different condo project contains condos of only two or three different types or price ranges.
A library of blueprints
There is a large library of blueprints at the contractor's central office. This library contains blueprints for all of the different types of condos that the contractor has built or is building. (This library is analogous to the class libraries available to the Java programmer.)
A subset from the blueprint library
When a condo project begins, the contractor delivers copies of several sets of blueprints to the construction site. The blueprints delivered to that site describe only the types of condos being constructed on that site.
Condo is analogous to an object
Each condo unit is analogous to an ordinary Java object.
Each set of blueprints delivered to the construction site is roughly analogous to an object of the class named Class. In other words, each set of blueprints describes one or more condo units constructed from that set of blueprints.
When construction is complete
When the construction project is complete, the contractor delivers a set of blueprints for each type of condo unit to the management firm that has been hired to manage the condo complex. Each set of blueprints continues to be analogous to an object of the class named Class. The blueprints remain at the site of the condo units.
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!



