next up previous
Next: Scope of instance variables Up: Unit 03 Previous: Rules for accessing the

Instance variables

Instance variables are variables defined in a class, but outside the body of methods. The declaration of an instance variable is similar to the declaration of a local variable of a method, but:

  1. the variable is defined inside the class, but outside all methods;
  2. the variable is preceded by an access modifier (usually private);
  3. the variable is always initialized when the object is created, either implicitly (to a default value), or explicitly by the constructor (see later).

    Note: This is different from local variables, which are not necessarily initialized when the associated memory location is created.

The instance variables are associated to the single objects and not to the entire class. This means that each object has its own instance variables. Two different objects have different instance variables.


next up previous
Next: Scope of instance variables Up: Unit 03 Previous: Rules for accessing the