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
To help you keep track of thing in a message-passing sense, you can pretend that there is a global reference variable whose name is the same as the name of a class.
This (hypothetical) reference variable contains a reference to the Class object that owns the class variable. Using standard Java message-passing syntax, you can access the class variable by joining the name of the reference variable to the name of the class variable with a period. Example syntax is shown below:
ReferenceVariableName.ClassVariableName
As a result of the hypothetical substitution process that I described above, this is equivalent to the following:
ClassName.ClassVariableName
We will see an example of this in the sample program that I will discuss later.
Be careful with this thought processCharacteristics of class methodsWhile this thought process may be useful when thinking about static variables and methods, I want to point out, that the thought process breaks down very quickly when dealing with Class objects in a deeper sense.
For example, when invoking the getName method on a Class object, an actual reference of type Class is required to access the members of the Class object. The name of the class will not suffice.
If this discussion of a global reference variable whose name matches the name of the class is confusing to you, just forget it. Simply remember that you can access class variables by joining the name of the class to the name of the class variable using a period as the joining operator.
I'm not going to talk very much about instance methods and class methods in this lesson. However, there are a couple of characteristics of class methods that deserve a brief discussion in this context.
Cannot access instance members
First, the code in a class method has direct access only to other static members of the class.
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!



