next up previous contents
Next: Instance and Class Methods Up: Encapsulation Previous: Encapsulation   Contents

Instance and Class Variables

In our program we have only instance variables and instance methods. We have to discuss about class variables and class methods. What are they? Why we need them?

Instance variables are defined such that each instance of the class, or object has its own set of variables. These variables are fully independent and stored in their own memory space. To get access to an instance variable the joining operator is used, in JAVA it is a simple period called dot operator. E.g., in our program we can access the name of the object obj as obj.name.

It is possible to surmise:

Class variables are shared among all objects of the class. They are very similar to global variables. Only one copy of a class variable exists in memory and all objects of the class can access them.

A very important characteristic of class variables is, that they can also be accessed even if no object of the type was instantiated. You can access them with dot operator, joining class name and the variable.


next up previous contents
Next: Instance and Class Methods Up: Encapsulation Previous: Encapsulation   Contents
Olexiy Ye. Tykhomyrov 2001-12-16