File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When to use a private static method?

 
Enrico Oliosi
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If I want to declare a class private method, is it better declare it static or not?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Guido Granobles
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47232
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand what a static method really is?
 
Guido Granobles
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic