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 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 variable
Every 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 object's 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
More importantly, you can also access a class variable without a requirement to go through an object instantiated from the class. (In fact, a class variable can be accessed in the total absence of objects of that class.)
(Remember, this discussion is conceptual in nature, and may not represent an actual implementation.)Assuming that a class variable is otherwise accessible, you can access the class variable by sending an access request message to the Class object to which the variable belongs.
One way to think of this
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!



