Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • @assylias: "the class will be final because it can't be subclassed". It depends on your definition of "final". It's not as if the class was implicitly made final because of the private constructor (in the way, say, interface methods that aren't declared as public are implicitly public). Putting for example public final or simply public shall lead to two different .class files being generated: with final the .class has its access flags set to 0x31 (public final) while with only public its access flags are 0x21. You cannot extend it but it's still not final... Commented Mar 8, 2012 at 14:14
  • @ArtB But you would agree, that the following is also nonsens?: The alternative would be, to write an "object-class" and call the constructor once, which does all the work... There is no point in using this object any further. It has no destructor, no getter, no setter... The object is pointless in this case, because all objects will be exactly the same. This is exactly, what static methods are for. And it is good practice, to encapsulate stuff into separat classes (i.e. "files"). Of course I can stuff everything in my calling class. But that would be too much... Commented Mar 8, 2012 at 14:30
  • @Sauer It's hard to offer an alternative without knowing specifics, but after working on becoming better with object-oriented design principles I find that very rarely are static methods needed. The only cases I can really justify are those where you are adding methods that ought to have existed in the class or are for dealing with null issues. Commented Mar 8, 2012 at 15:13
  • 1
    @ArtB Of course it is up to you, how you design your structure. But to me, static methods are a very valid part of each object oriented programm. I also write APIs at my company and I often use static methods to ENFORCE object oriented coding. Static methods are used to handle "static" and complex algorithms which are working WITH objects. That keeps the real object classes less complex and easier to understand (pojos). Sometimes there is no point in stuffing complex algorithms into object classes... Commented Mar 13, 2012 at 14:37
  • 1
    @Sauer I would recommend you at least try stuffing those algorithims into objects as an excercise. I've found that over time I've found neat ways to include those algorithims in to objects, or that some design pattern solves this. I suggest that it maybe be correct more often than you suspect to push things into objects from static methods. YMMV Commented Mar 13, 2012 at 21:16