aspose file tools*
The moose likes Java in General and the fly likes When to use a private static method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "When to use a private static method?" Watch "When to use a private static method?" New topic
Author

When to use a private static method?

Enrico Oliosi
Greenhorn

Joined: Aug 26, 2010
Posts: 10

If I want to declare a class private method, is it better declare it static or not?
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

If the method uses instance variables then you can't declare it static. It depends on the situation. Does the method something with the instance? Yes then don't declare it static.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Guido Granobles
Greenhorn

Joined: Sep 11, 2010
Posts: 22
I really do not see what useful would be having a method as static and private at the same time. It suppose that I have static methods when I want invoke them from wherever place of my code. but if it is private then I can invoke it just from within the class that it has been declared. Beside that such method just can use static variables. the only reason that comes to my mind for to have such method would be to invoke a method which will do something special thing just for that class that it belongs and it will return a value. But even doing it like this it does not make sense for me to have that combination private and static.


http://guido-granobles.blogspot.com
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Enrico Oliosi wrote:
If I want to declare a class private method, is it better declare it static or not?

First, there is trouble in your wordings. Are you considering about declare method as private? If so, those are two different purposes. I means the use of static and private!
Enrico Oliosi
Greenhorn

Joined: Aug 26, 2010
Posts: 10
Abimaran Kugathasan wrote:
First, there is trouble in your wordings. Are you considering about declare method as private? If so, those are two different purposes. I means the use of static and private!


Is it better declare it "private static" or only "private"?

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Enrico Oliosi wrote:Is it better declare it "private static" or only "private"?

As Wouter said, it depends on your scenario. Could you please elaborate a bit? What do you want?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Guido Granobles wrote:I really do not see what useful would be having a method as static and private at the same time.


Imagine a rather complex public static method. You could simplify it by breaking it down into simpler steps, and putting the steps into private methods, then calling them from the public method. Those private methods would need to be static, too.


[Jess in Action][AskingGoodQuestions]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Do you understand what a static method really is?
Guido Granobles
Greenhorn

Joined: Sep 11, 2010
Posts: 22
Ernest Friedman-Hill wrote:
Guido Granobles wrote:I really do not see what useful would be having a method as static and private at the same time.


Imagine a rather complex public static method. You could simplify it by breaking it down into simpler steps, and putting the steps into private methods, then calling them from the public method. Those private methods would need to be static, too.


Well, that make sense.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When to use a private static method?