Kit
12-08-2003, 05:51 PM
I still don't quite understand what it means to have a method be static in Java, can anyone explain? Thanks.
![]() | View Full Version : static in java Kit 12-08-2003, 05:51 PM I still don't quite understand what it means to have a method be static in Java, can anyone explain? Thanks. anglo_aussie 12-08-2003, 06:02 PM A class can be instanciated to create one or more objects. If you have a static method or propery - this is stored in the class only and not in any of the instanciations of that class. This is useful for storing the number of instances in memory (a counter that is incremented on creation (constructor) and decremented on deletion (destructor)). I.E. class Blah { counter = static int; public void Blah () { Blah.counter++; } public int static numInstances() { return counter; } ... Everytime you "new Blah()" - counter is incremented. You can get the number of instanced from Blah.numInstances() -- note the bit before the . is the class name, not an object name. You might also want to have static methods for performing functions related to the class without having to create an instance of the object. A popular example of this is the Integer.toString() - you don't need to create an object of type Integer to use this method. |