手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>Java技术>列表

The Essence of OOP using Java: Static Members

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

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. ...

Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded ..."

What does this mean?

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.

RTTI

Thus, information regarding the construction, wiring, plumbing, air conditioning, etc., for each condo unit (object) continues to be available at the site even after the construction has been completed.

(This is somewhat analogous to something called runtime type information and often abbreviated as RTTI. A Class object contains RTTI for objects instantiated from that class.)
What are those analogies again?

In the above scenario, each condo unit is (roughly) analogous to an object instantiated from a specific class (set of blueprints).

Each set of blueprints remaining onsite after construction is complete is roughly analogous to a Class object that describes the characteristics of one or more condo units.

What do you care?

Until you get involved in such advanced topics as reflection and introspection, you don't usually have much involvement or much interest in Class objects. They are created automatically, and are primarily used by the Java virtual machine during runtime to help it do the things that it needs to do.

An exception to that rule

However, there is one area where you will be interested in the use of these Class objects from early on. You will be interested whenever variables or methods in the class definition are declared to be static.

Class variables and methods

According to the current jargon, declaring a member variable to be static causes it to be a class variable.

(Note that local variables cannot be declared static. Only member variables can be declared static.)
Similarly, declaring a method to be static causes it to be a class method.

Instance variables and methods

On the other hand, according to the current jargon, not declaring a variable to be static causes it to be an

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!