the essence of oop using java: static members
preface
this miniseries will describe and discuss the necessary and significant aspects of oop using java. if you have a general understanding of computer programming, you should be able to read and understand the lessons in this miniseries, even if you dont have a strong background in the java programming language. 【程序编程相关:Servlet和JSP概述】
this lesson is one of a series of lessons designed to teach you about the essence of object-oriented programming (oop) using java. the first lesson in the group was entitled java/article/0,,12082_935351,00.html">the essence of oop using java, objects, and encapsulation. that lesson, and each of the lessons following that one, has provided explanations of certain aspects of the essence of object-oriented programming using java. the previous lesson was entitled java/sdjcetp/article/0,,12397_1015511,00.html">the essence of oop using java, polymorphism and interfaces, part 2.necessary and significant aspects 【推荐阅读:Struts学习笔记[3]使用ant来发】
i recommend that you also study the other lessons in my extensive collection of online java tutorials. you will find those lessons published at java">gamelan.com. however, as of the date of this writing, gamelan doesnt maintain a consolidated index of my java tutorial lessons, and sometimes they are difficult to locate there. you will find a consolidated index at java.html">baldwins java programming tutorials. 【扩展信息:JSP 设置HTTP应答头】
supplementary material
preview
static membersthere is another aspect of oop in java that i have avoided up to this point in the discussion: static variables and static methods.
tends to complicate ...
i have avoided this topic because, while not particularly difficult, the existence of static members tends to break up the simple structures that i have discussed in previous lessons in this miniseries.
while static members can be useful in some situations, the existence of static members tends to complicate the overall object-oriented structure of java.
avoid overuse of static members
furthermore, the overuse of static members can lead to problems similar to those experienced in languages like c and c++ that support global variables and global functions.
when to use static members
i will discuss the use of static members in this lesson, and will provide some guidelines for their use.
the class named class
i will also introduce the class named class and discuss how it enters into the use of static variables and methods.
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 ordinary objectsall 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 im not going to get into that at this time.)
array objects
i havent 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
class objectslet 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. ...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 analogyhere 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 contractors 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 dont 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 instance variable, and not declaring a method to be static causes it to be an instance method.
in general, we can refer to them as class members and instance members.
what is the difference?
here are some of the differences between class and instance members insofar as this discussion is concerned.
how many copies of member variables exist?
every object instantiated from a given class has its own copy of each instance variable defined in the class.
(instance variables are not shared among objects.)however, every object instantiated from a given class shares the same copy of each class variable defined in the class. (it is as though the class variable belongs to the single class object and not to the individual objects instantiated from that class.)access to an instance variableevery object has its own copy of each instance variable (the object owns the instance variable). therefore, the only way that you can access an instance variable is to use that objects reference to send a message to the object requesting access to the variable (even then, you may not be given access, depending on access modifiers).
why call it an instance variable?
according to the current jargon, an object is an instance of a class.
(i probably told you that somewhere before in this miniseries. at least, i hope that i did. after writing eight or ten lessons in a miniseries, i sometimes forget what i told you before.)each object has its own copy of each non-static variable. hence, they are often called instance variables. (every instance of the class has one.)access to a class variable
you can also send a message to an object requesting access to a class variable that the object shares with other objects instantiated from the same class. (again, you may or may not gain access, depending the access modifiers).
access using the class object
... 下一页