-1

I modeled the following off of something I inherited and am upgrading. What you are seeing is really just about as simply as what I'm working on. Why make makeAnObj() static? What does being static provide or offer that not being static doesn't?

public interface IsomethingElse {  int mbr(); }

public class obj : IsomethingElse {
    public static obj makeAnObj() { return new obj(); }
    public int mbr() { return 1; }
}

Edit: I'm repeating my reply from the thread below for additional background. his question is not an exact duplicate of the one being referred to. I'm not asking what a static f() is. My point is, look at what makeAnObj() does. I don't need a method to do create a obj (unless the method is also going to do something more, in this case the method isn't adding any addition value in the code.) The only value of this static is the programmer can create an instance by calling obj.makeAnObj() instead of new obj; far less readable IMO.

Steve
  • 905
  • 1
  • 8
  • 32
  • 1
    the name gives it away. to call a non static method you must have an instance of the object. The static methods are called on the class alone, a classic use case is a method that creates an instance – pm100 Feb 27 '17 at 17:13
  • 2
    It's probably a factory: a static (not bound to instance but to type itself) method that creates new objects. Probably obj's constructor is not marked public thus forcing the consumers of the class to use provided factory method. – Sergey.quixoticaxis.Ivanov Feb 27 '17 at 17:13
  • Thanks. I get all you are saying. And yes, but this isn't part of a factory. This question is not an exact duplicate of the one being referred to. I'm not asking what a static f() is. My point is, look at what `makeAnObj()` does. I don't need a method to do create a `obj` (unless the method is also going to do something more, in this case the method isn't adding any addition value in the code.) The only value of this static is the programmer can create an instance by calling `obj.makeAnObj()` instead of `new obj;` far less readable IMO. – Steve Feb 27 '17 at 17:22
  • My guess is, the person who wrote the code for the class wants you to use the factory method, so that they _can_ add extra stuff to the method in the future without needing to track down all the places that create the object directly. It's true there isn't really much benefit to the static method right now. – Hannele Feb 27 '17 at 18:27
  • 1
    @Hannele I came to believe either 1) other static methods existed at some point between the original release and now but were removed on some interim maintenance event -or- 2) the original intention was this class would someday have more static methods added but were never realized. But like I was trying to say there are not any relevant factories in this app. Thanks – Steve Feb 28 '17 at 19:38

1 Answers1

-3

In the code shown the static really isn't adding much value. Just do a new obj. In this case obj.makeAnObj() is little more than an alias for new obj(). I much prefer the later

Steve
  • 905
  • 1
  • 8
  • 32