The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

advantages/disadvantages of static methods?

just out of curiosity... which style is more desirable and why?

A) a static method in a class returning a value called like this:
String myString = StringGetterClass.getString();

B) or, a normal public method in a class that must be instantiated like so:
String myString = new StringGetterclass().getstring();

can anyone provide reasoning and/or scenarios where one is more appropriate than another? I'd be much appreciative!

I should totally know this
Monday, August 01, 2005
If you can make StringGetterClass sealed with a private constructor - (A) gives you cleaner code.

If for some reason you do not want to do it - (B) is the only option.

One of scenarios for (B) can be if you have stateless classes in data access and service layers and made an architectural decision to inherit services from data accessors.

Example for (A) - hierarchical structure for global constants'.
Monday, August 01, 2005
What sort of 'strings' are you gertting and how many ways can you represent it?

If you need to parametrize the function to specify the type of string to get you could do it to either, but in the second case you could also parameterize the constructor and have various access methods

Tuesday, August 02, 2005
If the getString() method requires no state, why would you want the overhead of an instantiated object?
Andrey Butov Send private email
Tuesday, August 02, 2005
Static methods allow you to call them when a particular instance of the object is not around yet.

The benefit? For example, Symbian OS uses static methods to create its own system of object instantiation. Instead of using "new" directly which could potentially fail in a resource-contrained environment like a cellphone, you make calls to static methods of classes to create an instance. Each class has a static method called "NewL()" that eventually calls the constructor. Until the constructor is called, before every attempt to allocate memory, you push the pointer onto a stack in case the operation fails. That way all allocated memory is accounted for whether the call fails or not and you get to avoid memory leaks.

Another situation is if you want to have a class of utility functions that you need in your program all over the place, but you don't want to make them global functions like you would in C. You pack them all in a class, make them static and call them like Utility::DoSomething().
Tuesday, August 02, 2005
String message = NLS.getLocalizedString("DE", UserNameLabel);
Dino Send private email
Tuesday, August 02, 2005
Wouldn't this...

String myString = new StringGetterclass().getstring();

be much safer in a multi threaded environment?
Tuesday, August 02, 2005
(These apply to Java.  I can't vouch for how much they apply for C# or C++.)

Advantages of a static method:

There's no need to create an object first.  The method is available immediately.

It's a good when you have generic functionality that does not depend on the state of a particular object.  For example, look at the Arrays class or the Collections class from java.util.

Static methods can be good for factories.  Pass your requirements as parameters, get a brand new object back.  Not a constructor in sight.

Disadvantages of a static method:

You don't have an object instance, so you only have access to static members and your own local variables.  If you want an instance, you probably have to create it yourself.

You can create subclasses, but a static method can't be abstract, so it's harder to decouple your implementation from a caller.
Christian Murphy Send private email
Wednesday, August 03, 2005
Christian is right on the money there.

The only thing I would add is that (in Java) you cannot have an interface define static methods. Thus, if you are using static methods you are bound to the specific implementation in the code. In some cases, especially if you are distributing an application to multiple clients, this is not an option.
Andrew Knock Send private email
Thursday, August 04, 2005
"There's no need to create an object first.  The method is available immediately."

Assuming the class is already loaded.  Otherwise there's a bit of a wait.  :-)

I think of static as a good way to separate the functional code from procedural/state-setting code.  The functional code typically needs no extension and changes only when there are bugs.

There's also the use of static as an access-control mechanism--such as with singletons.
Matt Brown Send private email
Thursday, August 04, 2005
Static methods can be used as factories, like many people here have mentioned.  What they haven't mentioned is that this use of static factories doesn't force the end-user to know what kind of object they got back.  This is often used to separate the implementation from the interface.  Some people have actually liked this method so much, they'd rather have it than constructors.  That's probably overkill, but it has it's points.

Another use is a very functional-style programming.  This has been mentioned before on this thread.  One advantage of this is that (at least in Java) the static class has access to any private or protected members in any parameters of the same type you choose to send it.  Of course, if you pass in a parameter of the same type as the class, the reasons for not making it a regular method are obscure :).
Thursday, August 04, 2005
I'd like to thank everyone who answered this post. I had a solid understanding (and RTFM about) traditional public methods within instantiated classes, as well as static methods, and after quite a bit of googling, I could find no real authoratative resource as to when/where to use one over the other, and the considerations behind that reasoning.

Your answers were dead on and really answered my question clearly, succinctly, and skillfully.

I think this speaks without a question to your collective skills and willingness to share the knowledge you have acquired with others.

Given time, I hope to do the same.

"I should totally know this"
I should totally know this
Saturday, August 06, 2005
If anyone still on this topic, may be worth to take a look about this link:
Carfield Yim Send private email
Tuesday, August 09, 2005
In case your code needs to talk to VB6, static is not recognized by VB6 in my findings.
Luc Send private email
Tuesday, August 23, 2005

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz