suppose, i have to pass a parameter to a method and the method should return a value. so what would be a good design ?
declaraing a static method or declaraing a simple method ? which is one is better ? both of them will work . is not it ? but which one is better ? and why ?
If your method does not depend on the state of the class, aka: doesn't use member-variables, then it's a good idea to make this visible by declaring your method static.
Other Classes may use this method without creating an object of this class first, which will be confusing, if that object is never used again.
Perhaps this is a minor performance benefit (no ctor, no gc needed).
But if your design might be evolved, and later implementations might depend on an inner state of an object, it will be harder to refactor.
Perhaps from the logic of the program, there is allways an object of that class available.
If your method does not depend on the state of the class, aka: doesn't use member-variables, then it's a good idea to make this visible by declaring your method static.
Other Classes may use this method without creating an object of this class first, which will be confusing, if that object is never used again.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.