手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>程序设计>Java技术>列表

The Essence of OOP using Java: Static Members

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

(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 careful

Once 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.

No object required

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 1

Two 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){



Listing 2

The important thing to note here is that the main method is declared static. That causes it to be a class method.

As a result, the main method can be invoked without a requirement for an object of the class to exist.

(Also, the main method has direct access only to other static members.)
How a Java application starts running

In fact, that is how the Java Virtual Machine starts an application running.

First the JVM finds the specified file having an extension of .class. Then it examines that file to see if it has a main method with the correct signature. If not, an error occurs.

If the JVM finds a main method with the correct signature, it invokes that method without instantiating an object of the class. That is how the Java Virtual Machine causes a Java application to start running.

A side note regarding Applets
For those of you who are familiar with Java applets, you should know that this is not the case for an applet. You should know that an applet does not use a main method. When an applet is started, an object of the controlling class is instantiated by the browser, by the appletviewer program, or by whatever program is being used to control the execution of the applet.
A poor programming technique

Basically, this entire sample program is coded inside the main method. As a practical manner, that is a very poor programming technique, but it works well for this example.

Display some text

The code in Listing 3, which is the first executable statement in the main method, causes the words Static variable to appear on the computer screen. I will come back and discuss the details of this and similar statements later in the lesson.

    System.out.println(

                    "Static variable");




文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!