STATIC VS. INSTANCE WHAT STATIC MEANS AND WHEN TO USE IT
Instance Items • Ordinary attributes and methods of a class are called “instance” items because every object (instance) of the class has its own copy • In the example to the right, the attributes name and age, and the methods Person, get. Age, get. Name, and to. String are instance items • Every object created from this class has its own name, its own age, its own methods, and so forth
Static Items • A static item is one that is shared by every instance (object) of the class • There is only one of each static item – not one per object • The single static item belongs to the class, class not to any object of the class • It is referenced via the class name rather than through an object name • Examples: Circle. PI, Circle. PI Circle. number. Of. Circles • In the code to the right, there is only one PI for the whole class, and there is only one number. Of. Circles for the whole class, but every Circle object has its own radius
Driver Class • The driver class has only static items including the main method – because there is only one main method and only one driver • All attributes of the driver must be static because static methods can ONLY refer to static items and local items defined inside a static method • All methods must be static because there is only one driver and because the main method is static • Thus other methods in the same driver class cannot be referenced from main (or from each other) unless they are static