What's Next?
Complete Program Listing
/*File MyClass01.java
Copyright 2002, R.G.Baldwin
This program illustrates static
members of a class. Output is:
Static variable
Mon Sep 17 09:52:27 CDT 2001
Instance variable
Mon Sep 17 09:52:32 CDT 2001
Static variable
Mon Sep 17 09:52:27 CDT 2001
Instance variable
Mon Sep 17 09:52:37 CDT 2001
Static variable
Mon Sep 17 09:52:27 CDT 2001
**************************************/
import java.util.Date;
class MyClass01{
static Date v1 = new Date();
Date v2 = new Date();
public static void main(
String[] args){
//Display static variable
System.out.println(
"Static variable");
System.out.println(MyClass01.v1);
//Delay for five seconds
try{
Thread.currentThread().sleep(5000);
}catch(InterruptedException e){}
//Instantiate an object and
// display instance variable
MyClass01 ref1 = new MyClass01();
System.out.println();//blank line
System.out.println(
"Instance variable");
System.out.println(ref1.v2);
//Now, display the static
// variable using object reference
System.out.println(
"Static variable");
System.out.println(ref1.v1);
System.out.println();//blank line
//Delay for five seconds
try{
Thread.currentThread().sleep(5000);
}catch(InterruptedException e){}
//Instantiate another object
MyClass01 ref2 = new MyClass01();
System.out.println();//blank line
System.out.println(
"Instance variable");
System.out.println(ref2.v2);
//Now, display the same static
// variable using object reference
System.out.println(
"Static variable");
System.out.println(ref2.v1);
}//end main
}//end class MyClass01
//===================================//
Listing 13Preface
Necessary and significant aspects
This miniseries will describe and discuss the necessary and significant aspects of OOP using Java. If you have a general understanding of computer programming, you should be able to read and understand the lessons in this miniseries, even if you don't have a strong background in the Java programming language.
Supplementary material
I recommend that you also study the other lessons in my extensive collection of online Java tutorials. You will find those lessons published at Gamelan.com. However, as of the date of this writing, Gamelan doesn't maintain a consolidated index of my Java tutorial lessons, and sometimes they are difficult to locate there. You will find a consolidated index at Baldwin's Java Programming Tutorials.
Preview
There is another aspect of OOP in Java that I have avoided up to this point in the discussion: static variables and static methods.
Tends to complicate ...
I have avoided this topic because, while not particularly difficult, the existence of static members tends to break up the simple structures that I have discussed in previous lessons in this miniseries.
While static members can be useful in some situations, the existence of static members tends to complicate the overall object-oriented structure of Java.
Avoid overuse of static members
Furthermore, the overuse of static members can lead to problems similar to those experienced in languages like C and C that support global variables and global functions.
When to use static members
I will discuss the use of static members in this lesson, and will provide some guidelines for their use.
The class named Class
I will also introduce the class named Class and discuss how it enters into the use of static variables and methods.
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




