Here’s an OO question for everyone who’s reading out there, when do you use static private methods?

That a method is private implies that it’s only going to be used within that class; that a method is static means that it’s operation in now depends on the state of your object instance. For example, in writing lots of CGI type stuff recently, I’ve been doing Ye Old Extract Method on many little things; that is, instead of in-lining logic to, say, build up a log message in a StringBuffer, I just make a method like erroLogMessage(...).

Now, these methods really don’t need to dink around with the instance at all: they get everything they need from the formal parameters. So, you might think, well, this could be a static method. But is that wise?

The thinking I have here is that if a private method doesn’t need access to the instance, then I make it static. Following this thinking, in doing servlet work — where you’re basically doing procedural/static programing in one big ass method — I end up creating a lot of private static methods, and it looks kind of wierd.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.