Wednesday, March 30, 2011

Java Instance variables and Class variables

aka static/non-static variables.

In object-oriented programming with classes, an instance variable is a variable defined in a class (i.e. a member variable), for which each object of the class has a separate copy. They live in memory for the life of the class.

An instance variable is the opposite of class variable, and it is a special type of instance member.

A class variable is a variable defined in a class (i.e. a member variable) of which a single copy exists, regardless of how many instances of the class exist. We use keyword static for declaring class variables.

Instance variable:
■ Can be marked final
■ Can be marked transient
■ Cannot be marked abstract
■ Cannot be marked synchronized
■ Cannot be marked strictfp
■ Cannot be marked native
■ Cannot be marked static, because then they'd become class variables.

No comments:

Post a Comment