The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Classes and Inheritance

Understanding Instance and Class Members

You learned briefly about instance and class members in Language Basics (in the Learning the Java Language trail). 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, and main, which is a class method:
import java.util.*;

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.format("%s%n", anInstance.instanceMethod());
        System.out.format("%s%n", anotherInstance.instanceMethod());

        //Illegal to refer directly to instance members from a class method
        //System.out.format("%s%n", instanceMethod());    //illegal
        //System.out.format("%s%n", instanceInteger);     //illegal

        //Refer to class members through the class...
        AClass.classInteger = 7;
        System.out.format("%s%n", classMethod());

        //...or through an instance. (Possible but not generally recommended.)
        System.out.format("%s%n", anInstance.classMethod());

	//Instances share class variables
        anInstance.classInteger = 9;
        System.out.format("%s%n", anInstance.classMethod());
        System.out.format("%s%n", anotherInstance.classMethod());
    }
}
Here's the output from the program:
1
2
7
7
9
9
The following figure shows the objects and member variables in the program and how they are related.

Objects in the AClass Example

Objects in the AClass Example

Unless otherwise specified, a member declared within a class is an instance member. So instanceInteger and instanceMethod 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 by anInstance and anotherInstance each have their own copy of instanceInteger. 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 marked illegal and try to compile the program, the compiler will display an error message.

A class member is declared by using the static modifier. Besides the main method, AClass declares one class variable and one class method, called classInteger and classMethod, 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 of classVariable, its value changes for all instances.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.