Download presentation
Presentation is loading. Please wait.
1
What Static means and when to Use it
Static vs. Instance What Static means and when to Use it
2
Instance Items Ordinary attributes and methods of a class are called “instance” items because every object of the class has its own copy In the example to the right, the attributes name and age, and the methods Person, getAge, getName, and toString are instance items Every object created from this class has its own name, its own age, its own methods, and so forth
3
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, not to any object of the class It is referenced via the class name rather than through an object name Examples: Circle.PI, Circle.numberOfCircles In the code to the right, there is only one PI for the whole class, and there is only one numberOfCircles for the whole class, but every Circle object has its own radius
4
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 cannot be referenced from main (or from each other) unless they are static
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.