Pregunta

Say I defined a standalone static function. No need to access any outside variables -- completely self-contained.

Are there any performance penalties/benefits to consider when defining it, or should I just stick it wherever it makes the most sense?

(This is by far the function that takes up the most CPU time, so I don't want to be inadvertently hurting myself.)

¿Fue útil?

Solución

Put it wherever it makes the most sense from the point of your program organization. Static fields and methods are loaded only once per class loader. There's no way it could hurt the performance in the run time.

Otros consejos

No, it doesn't matter where in the class it is defined. All you're doing is defining a method - it'll take the same amount of time whether it's at the beginning or end or somewhere in between. You can verify this with a profiler. It's executing the function that will potentially need to be optimized if anything.

You don't need to worry about methods in your class trying to use it "before it's defined" either, since your entire class gets classloaded before you can run any methods in it.

Static methods are useful in the way that they dont need an Object to invoke the method.They belong to the class as they are class variables.As such,static methods are not involved in increasing your heap memory size

Everything else is same as a normal instance method

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top