When to use static method in a java class

时间:2023-03-09 03:59:34
When to use static method in a java class

First , please understand its feature :

* no need to instantiate a instance, i.e. simply you can just write: AutoTrace.start();

* All instances will share one static method, consider the consistency when the method operate a static (global) variable. [ as to its local variables, each thread has self stack. This's similar to non-static method ]

Then to consider whether to declare as static :

* If you really no need to construct a java instance before call its method

* the static variable of the static method is immutable or not / no need to consider its consistency [ i.e. locking ]

....