113

I just wanted to clear up a question I have. What is the point of having a private static method as opposed to a normal method with private visibility?

I would have thought an advantage to having a static method is that it can be called without an instance of a class, but since its private is there even a point to it being static?

The only reason I can think of is that it helps conceptually understanding the method on the class level as opposed to object level.

Deduplicator
  • 9,209
Rehan Naqvi
  • 1,249

9 Answers9

90

The characteristic of being static is independent of the visibility.

The reasons that you will want to have a static method (some code that does not depend on non-static members) will still be useful. But maybe you don't want anyone/anything else to use it, just your class.

36

A fairly common reason (in Java) would be for initializing immutable field variables in a constructor by using a simple private static method to reduce constructor clutter.

  • It is private: external classes should not see it.
  • It is static: it can perform some operation, independent1 of the state of the host class.

A somewhat contrived example follows...

eg:

public class MyClass{
    private final String concatenated;

    public MyClass(String a, String b){
        concatenated = concat(a,b);
    }

    public String getConcatenated(){
       return concatenated;
    }

    /**
    *  Concatenates two Strings as `s1---s2`
    **/
    private static final String concat(String s1, String s2){
        return String.format("%s---%s", s1, s2);
    }
}

1 Assuming it has no interaction with other static variables.

29

A common use-case for a private static method is a utility method which is

  1. only used by that one class
  2. is independent of the internal state of that class
Philipp
  • 23,488
22

You see that you have a few lines of code that is repeated in a lot of your methods, so you decide to extract them to a single method, as duplicated code is not good.

You make the method private as it is not designed for widespread usage and you don’t want unrelated code calling it. (Debate this point in the comments….)

As the method does not access any instance fields, it can be make static, by making it static you make it easier to understand and maybe even a little faster.

Then.... (Maybe now, maybe later, maybe never)

Once the method has been made static, it is clear that it can be moved out of the class, say to an unity class.

It is also easy to transform it into an instance method of one of its parameters, often this is where the code should be.

Ian
  • 4,623
3

I can think of at least two reasons why you would need a static private method on a class.

1: Your instances have reason to call a static method you don't want called directly, perhaps because it shares data between all instances of your class.

2: Your public static methods have subroutines that you don't want called directly. The method is still called without an instance, just not directly.

Of course, "it helps the class make sense" is a fine reason all on its own.

DougM
  • 6,379
3

Generally, if I find I'm writing private static methods, I take it as an indication that there's something I should have modeled separately.

Since they're not tied to the state of a particular object instance, a collection of public and private static methods could form an entirely separate class with its own semantics & non-static methods.

(Another tip-off is if I find I have a lot of static methods (public and private) that all have a common parameter of some type, they might be better off as members of that type.)

So, to answer your question, private static methods appear when a class provides a group of related methods that are independent of an instance of that class. (...and since they're independent, they may be better off in their own class.)

sea-rob
  • 6,911
2

“static” and “private” are orthogonal concepts. You want some methods to be static, and others to be instance methods. You want some methods to be private, others to be public. These are independent. If you have good reasons to make a method static, and good reasons to make it private, you make it private static or static private, which is the same.

gnasher729
  • 49,096
1

Static is always better than non-static and worse than a free functions, which is worse than a pure function.

Private is always better than protected which is better than public.

'Need to know' principle. this is a large place even if it does not include mutable fields.

Vorac
  • 7,169
-1

Very simple example I can think of is, if you want to do some processing on input arguments (or some manipulation) which are passed to main function. So in this case if processing is big and same functionality will not be used anywhere else it will make sense to have a private function as it will not be used/called from anywhere else + static as main is static.