Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
You learned briefly about instance and class members in Language Basics. This section shows you how to declare and use class and instance members. The following class,AClass
, declares an instance member variable, an instance method, a class variable, a class method, andmain
, which is a class method:Here's the output from the program:public class AClass { public int instanceInteger = 0; public int instanceMethod() { return instanceInteger; } public static int classInteger = 0; public static int classMethod() { return classInteger; } public static void main(String[] args) { AClass anInstance = new AClass(); AClass anotherInstance = new AClass(); //Refer to instance members through an instance. anInstance.instanceInteger = 1; anotherInstance.instanceInteger = 2; System.out.println(anInstance.instanceMethod()); System.out.println( anotherInstance.instanceMethod()); //Illegal to refer directly to instance members //from a class method //System.out.println(instanceMethod()); //illegal //System.out.println(instanceInteger); //illegal //Refer to class members through the class... AClass.classInteger = 7; System.out.println(classMethod()); //...or through an instance. System.out.println(anInstance.classMethod()); //Instances share class variables anInstance.classInteger = 9; System.out.println(anInstance.classMethod()); System.out.println(anotherInstance.classMethod()); } }The following figure shows the objects and member variables in the program and how they are related.1 2 7 7 9 9Unless otherwise specified, a member declared within a class is an instance member. So instanceInteger
andinstanceMethod
are both instance members. The runtime system creates one copy of each instance variable for each instance of a class created by a program. Thus, the objects referred to byanInstance
andanotherInstance
each have their own copy ofinstanceInteger
. You can access an instance member and call an instance method only through a reference to an instance. If you remove the two slashes from the beginning of the lines markedillegal
and try to compile the program, the compiler will display an error message.A class member is declared by using the
static
modifier. Besides themain
method,AClass
declares one class variable and one class method, calledclassInteger
andclassMethod
, respectively. The runtime system allocates a class variable once per class, regardless of the number of instances created of that class. The system allocates memory for a class variable the first time it encounters the class. All instances of that class share the same copy of the class’s class variables. You can access class variables either through an instance or through the class itself. Similarly, class methods can be invoked on the class or through an instance reference. Note that when the program changes the value ofclassVariable
, its value changes for all instances.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.