0

Although why static is evil has been discussed extensively, I cannot find an answer to this question:

What is the benefit (yes, I said BENEFIT--the keyword exists, doesn't it?) to declaring a method static? Is it merely shorter syntactically to call the method since you don't have to instantiate the object? Is there a performance or memory improvement when calling a static method many times versus creating many instances of a class and calling the method on each object? In other words, does creating 10,000 instances of a class take 10,000 times more memory than accessing a static method?

Joe
  • 376
  • 4
  • 13
  • I would be happy to reopen this if you [edit] your question to explain what is not answered by the duplicate I've linked here. – durron597 Aug 26 '15 at 01:43
  • Static methods should be made in meaning of class only like `String.valueOf()`, where you not consider using it in the meaning of object. Static methods also have place in object building patterns. – itwasntme Aug 26 '15 at 01:46
  • This related [entry](http://stackoverflow.com/questions/2671496/java-when-to-use-static-methods?lq=1) really explain everything. – Ferdinand Neman Aug 26 '15 at 01:56
  • @durron597 I've modified my question so it can be reopened. Please provide a response indicating if you will or will not reopen the question, and why. You were quick to close it (minutes after I posted). I hope you will be as quick to respond to my editing of the question. – Joe Aug 26 '15 at 17:23
  • @Justin What about your revised question is not explained by [this answer](http://stackoverflow.com/a/7084473/1768232) in the dupe and [this question](http://stackoverflow.com/questions/2671496/java-when-to-use-static-methods) linked by Ferdinand Neman? – durron597 Aug 26 '15 at 17:25
  • Also, you don't have to create 10000 instances. You can share the same instance via [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) and still not use static state. – durron597 Aug 26 '15 at 17:26
  • @Justin FYI three other people agreed with me this was still a duplicate **after the edit**. I'm not sure if you can see this link at 1 rep, but here's the [review task](http://stackoverflow.com/review/reopen/9287519) – durron597 Aug 26 '15 at 17:32
  • @durron597 Well, clearly the review process sucks, because the question linked to in the dupe banner is a different question from the revised question. Clearly, the reviewers didn't bother reading the edits. Ferdinand did give the correct duplicate. Are you suggesting that it doesn't matter what the dupe banner says as long as a link to the correct duplicate is burried in the comments? – Aleksandr Dubinsky Aug 26 '15 at 17:37
  • @AleksandrDubinsky The one in the dupe banner also adequately answers the question. You are welcome to post about it on [meta] if you like. – durron597 Aug 26 '15 at 17:39
  • @durron597 Honestly, this question is different from either of the linked-to questions. Justin's question is, "I've heard that the static keyword is evil. Is it evil to use static methods?" The answer is "What you heard is that static non-final variables are evil. Static methods are totally ok, as are static inner classes and static final variables. The keyword is not the problem." – Aleksandr Dubinsky Aug 26 '15 at 17:42
  • @AleksandrDubinsky I don't agree. If you care that much, post on [meta]. The questions may not be worded the same, but the answers have the same answers. – durron597 Aug 26 '15 at 17:44
  • @durron597 So are you saying that using 10,000 instances WOULD consume 10,000 times the memory of a static method? I accept that dependency injection may be a better solution and am doing some reading about it, but I still want to understand. – Joe Aug 26 '15 at 18:00
  • 2
    @AleksandrDubinsky Duplicates aren't about questions. The text states _This question already has an answer here_ Duplicates are about answers. The answers in the linked post answer this question. Whether the question was the same is irrelevant. – Sotirios Delimanolis Aug 26 '15 at 19:04
  • I don't know if it matters, or if this question will be visible to others in the future, but I found my answer on another post here: http://stackoverflow.com/questions/7673359/static-method-memory-allocation – Joe Aug 26 '15 at 22:55
  • @SotiriosDelimanolis You are incorrect. Duplicates are about questions, not answers. https://meta.stackoverflow.com/questions/292329/does-the-same-answer-imply-that-the-questions-should-be-closed-as-duplicate?lq=1 – Aleksandr Dubinsky Aug 27 '15 at 08:43

0 Answers0