Class members and instance members
Class variables and class methods are declared static (declaring a member static in the class definition causes to be called a class member).
Instance variables and instance methods are not declared static.
Each object has its own copy ...
Every object instantiated from a given class has its own copy of each instance variable declared in the class definition. (Instance variables are not shared among objects.)
Every object instantiated from a given class acts like it has its own copy of every instance method declared in the class definition. (Although instance methods are actually shared among objects in order to reduce the amount of memory required, they are shared in such a way that they don't appear to be shared.)
Every object shares ...
Every object instantiated from a given class shares the same single copy of each class variable declared in the class definition. Similarly, every object instantiated from a given class shares the same copy of each class method.
Accessing an instance member
An instance variable or an instance method can only be accessed by using a reference to the object that owns it. Even then, it may or may not be accessible depending on the access modifier assigned by the programmer.
Accessing a class member
The single shared copy of a class variable or a class method can be accessed in either of two ways:
- Via a reference to any object instantiated from the class
- By simply joining the name of the class to the name of the class variable or the class method
When to use class variables
It is very often appropriate to use final static variables, as constants in your programs. It is rarely, if ever, appropriate to use non-final static variables in your programs. The use of non-final static variables should definitely be minimized.
When to use static methods
It is often appropriate to use static methods in your programs, provided there is no requirement for the method to remember anything from one invocation to the next. Static methods should be self-contained.
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 13上一篇: eclipse插件安装
下一篇: 一个简单的用JAVA实现的屏幕抓图(源代码)
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!



