Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
23  
Yes, there's a reason. Your public/default static methods can still call your private statics. Whether static methods should be used at all, and when it's appropriate to use private methods of any kind, are separate questions so be careful not to mix them with this question. –  user39685 Apr 1 at 14:19
2  
Quick example - factory method used by an inner class preventing invocation by other classes (you've gotta go through the factory method to get an instance of the class). –  MichaelT Apr 1 at 14:21
2  
@MattFenwick: you should post this as an answer. –  Doc Brown Apr 1 at 14:23
7  
By making a method static, you are also saying that it does not read or write to instance variables. A method that doesn't interact with instance variables is often easier to move and refactor to other places. –  Mark Rogers Apr 1 at 15:56
1  
Don't forget that regardless of whether a static method is private, internal or public, it still isn't thread-safe without specific code synchronization effort on your part. –  Craig Apr 1 at 19:46

8 Answers 8

up vote 41 down vote accepted

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.

share|improve this answer
1  
I think you're on the right track, but I also think you're missing a key point. Most static methods have zero side effects (they're effectively functions, not methods) - that's why they're made static in the first place. The argument against them being private is "If they have no side effects, why not make them public to promote code reuse". That's the key point: when we make it private, it's usually because we want you to reuse the entire class that uses that private method, not just that method. –  corsiKa Apr 2 at 17:32
    
I must say I have never done that or seen that behaviour. The few times I have done it was for helper methods to public static classes. –  Miyamoto Akira Apr 3 at 22:06
    
@corsiKa: Side-effects aren't the issue. Making a static member of a class non-private effectively promises that every future version of a class will include that same method of the same name which does the same thing. If needs change, and a future version of the class may have no need for a method which works exactly like the current one does, a private method may safely be replaced with one that better fulfills the new needs of the class. As a simple example, suppose a class needs a method which takes a string and performs substitutions like < to &lt;, etc. When it is written... –  supercat Aug 29 at 17:27
    
...the strings which it is passed are known not to contain any ampersands, and it thus does not convert & to &amp; [were I writing the code, I'd convert & to &amp; even if I didn't expect it to be necessary, but suppose it doesn't]. It it later becomes necessary to handle ampersands, but the method is public, changing it to expand & to &amp; could break outside code which performs its own &-to-&amp; substitution. If the method is private, however, it could be fixed to handle & without causing problems for any outside code. –  supercat Aug 29 at 17:32
    
That's an interesting problem to say the least, but I don't see how it being static makes it any more of a problem than not being static, which is key to this issue. If we follow your logic, everything everywhere would reimplement everything because "omg, what if there's a bug or requirements change someday?" –  corsiKa Aug 29 at 17:38

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.

share|improve this answer
9  
This is even more valuable when you need to pass derived (calculated) arguments to a superclass' constructor. The super(...) call must be the first line of your class' constructor, so you can't declare local variables to help you work out what to pass to the super call. However, you can call (private) static methods, which use as many lines as is necessary to work out the value to pass to the super constructor. –  Andrzej Doyle Apr 2 at 7:25
    
note that there's the possibility of a static initializer block –  Franz Ebner Apr 2 at 15:40

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
share|improve this answer

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.

share|improve this answer

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.

share|improve this answer

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.)

share|improve this answer

The purpose of OOP is to help make the program easier to understand, by limiting the scope of parts of the program, the purpose of access modifiers is exactly he same.

So, static private is in a way the ideal state -- it tells you that nobody outside of the class needs know anything about it, and that inside method all state is contained in local variables and parameters.

Why would you want a method that doesn't rely upon the state in a class? Well, I once worked on a program that had to display three years worth of data. The data for one year was read in, written out to the UI, then the next years data was read, and written out to the UI, and then the final years data was read and written out to the UI. The only difference in these seperate blocks of code was the parameter used to get a specific years worth of records and what UI element was used. By using a private static method that took the UI elements as parameters, it was possible to get rid of the duplicated code and ensure that the elements were populated identically in all years.

Private = nobody else needs to be bothered to learn about it, static = state that is external to the method is irrelevant and you don't need to know about it when working on the method.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.