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

The Essence of OOP using Java: Static Members

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!
Since the two objects referred to by ref1 and ref2 came into existence with a five-second delay, the Date objects belonging to those two object reflect a five-second difference in the time encapsulated in the objects.

Only one copy of class variable exists

Also remember that if a variable is a class variable, only one copy of the variable exists, and all objects instantiated from the class share that one copy.

This is illustrated by the code in Listing 11, which uses the reference to the second object instantiated from the class named MyClass01, to cause the contents of the class variable named v1 to be displayed.

    System.out.println(ref2.v1);

  }//end main



Listing 11

The output produced by the code in Listing 11 is shown in Figure 5.

Mon Sep 17 09:52:27 CDT 2001



Figure 5

Same output as before

As you can see, this is the same as the output shown in Figure 1 and Figure 3 earlier.

Accessing the same physical class variable

Since only one class variable named v1 exists, and all objects instantiated from the class named MyClass01 share that single copy, it doesn't matter whether you access the class variable using the name of the class, or access it using a reference to either of the objects instantiated from the class. In all three cases, you are accessing the same physical class variable.

Since nothing was done to cause the contents of the class variable to change after it came into existence and was initialized, Figures 1, 3, and 5 are simply three different displays of the date and time encapsulated in the same Date object whose reference is stored in the class variable.

Let's revisit System.out.println...

Now, I want to revisit the statement originally shown in Listing 8 and repeated in Listing 12 for viewing convenience.

    System.out.println(ref1.v1);



Listing 12

Java programmer wanted

I often tell my students that if I were out in industry, interviewing prospective Java programmers, my first question would be to ask the prospective employee to tell me everything that she knows about the statement in Listing 12.

Covers a lot of Java OOP technology

This is not because there is a great demand for the use of this statement in real-world problems. (In fact, in a GUI-driven software product world, there is probably very little demand for the use of this statement.) Rather, it is because a lot of Java object-oriented technology is embodied in this single statement.

In that scenario, I would expect to receive a verbal dissertation of fifteen to twenty minutes in length to cover all the important points.

The short version

Let me give you the short version. There is a class named System. The System class declares three static (class) variables having the following types, names, and modifiers:

  • public static final PrintStream out
  • public static final InputStream in
  • public static final PrintStream err
(Note that these class variables are also declared final, causing them to behave as constants.)
Access the out variable without an object

Because out is a class variable, System.out returns the contents of the class variable named out (an object of the System class is not required in order to access a class variable of the System class).

In general, (ignoring the possibility of subclasses and interfaces) because out is a reference variable of type PrintStream, the returned value must either be null (no object reference) or a reference to a valid PrintStream object.

Object of the PrintStream class

When the Java Virtual Machine starts an application running, it instantiates an object of the PrintStream class and connects it to the standard output device.

(By default, the standard output device is typically the computer screen, but it can be redirected at the operating system level to be some other device. The following discussion assumes that the screen is the standard output device.)
Assign object's reference to out variable

When the PrintStream object is instantiated by the virtual machine, the object's reference is assigned to the class variable of the System class named out.

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