Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Public static methods vs public methods

What's the difference between a public static method and a public method? Why would you use a public static method?

like image 604
redconservatory Avatar asked Sep 30 '10 15:09

redconservatory


1 Answers

The methods of the Math class are static. So, in doing

Math.round(average)

the Math class itself is unchanged by what you've done - it only returns a value or acts upon the value you pass.

So - static methods are useful for utilities. Things like

StringUtils.removeWhitespaceFrom(textContent:String):String

or

BrowserUtils.openInNewWindow(url:String):void

It's very unusual that you'd use a static method for anything else. Don't use statics like 'getInstance()' to create Singletons - look into a framework for dependency injection instead.

like image 108
Stray Avatar answered Sep 22 '22 12:09

Stray