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.
(A class method does not have direct access to instance variables or instance methods of the class.)This is sort of like saying that a class method has access to the methods and variables belonging to the Class object, but does not have access to the methods and variables belonging to the ordinary objects instantiated from the class described by the Class object.
Once again, be carefulNo object requiredOnce again, this thinking breaks down very quickly once you get beyond static members. A Class object also has instance methods, such as getName, which can only be accessed using an actual reference to the Class object.
Now you are probably beginning to understand why I deferred this discussion until after I finished discussing the easy stuff.
Another important characteristic is that a class method can be accessed without a requirement for an object of the class to exist.
As with class variables, class methods can be accessed by joining the name of the class to the name of the method with a period.
I will illustrate much of this with a sample program named MyClass01.
Discuss in fragments
I will discuss the program in fragments. You will find a complete listing of the program in Listing 13 near the end of the lesson.
Listing 1 shows the beginning of the class definition.
class MyClass01{
static Date v1 = new Date();
Date v2 = new Date();
Listing 1Two member variables
The code in Listing 1 declares two member variables, named v1 and v2, and initializes each of those variables with a reference to a new object of the Date class.
(When instantiated using the constructor with no arguments, the new Date object encapsulates the current date and time from the system clock.)Note the static keyword
The important thing to note here is the use of the static keyword when declaring the variable named v1. This causes v1 to be a class variable, exhibiting the characteristics of class variables described earlier.
An instance variable
On the other hand, the variable named v2 is not declared static. This causes it to be an instance variable, as described above.
The main method is a class method
Listing 2 shows the signature for the main method.
public static void main(
String[] args){
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!



