y many java architects avoid static methods #threading

Hi XR,

Not sure why my architect doesn’t like static methods. I have some stateless method. It takes an object, pass to a static JNI method, then gets an output object, and returns it. I created a simple wrapper class around this single method. I feel making the method non-static requires callers to instantiate the class first. It would give the illusion that there’s state in the instance. It can also lead to unnecessary object proliferation.

My colleague mentioned threading as a reason. He’s not a newbie on threading, but I feel a stateless method is safest when it’s static. It is possible that he worries that this static method may one day access some static field? In that case, making it non-static and requiring every caller to have its own instance can help a bit.

Another colleague (elsewhere) also felt static methods are bad in threading. He’s also experienced in threading.

In threading, My habit is to keep things simple. If a method is stateless, i keep it static and have zero static field in that class. When people look at the code they immediately see it’s stateless, just like Math.abs().

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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