The Essence of OOP using Java: Static Members…
2008-02-23 09:45:59来源:互联网 阅读 ()
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){
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
