There are two types of variables or methods:
Instance
Static
An instance variable
is declared as a part of the class and is associated with an object. A static variable
is declared in the same way, except that it is preceded by the static
keyword. When an object is created, it has its own set of instance variables. However, all objects share a single copy of static variables.
Sometimes, it makes sense to have a single variable that can be shared and accessed by all instances of a class. When used with a variable, it is called a class variable and is local to the class itself.
Consider the following Employee
class:
public class Employee { // static variables private static int minimumAge; // instance variables private String name; private int zip; private int age; ... }
Each Employee
object will have its own copies of the name
, zip
, and age
variables. All Employee
objects may share the same minimumAge
variable. The use of a single...