• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When to use a private static method?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If I want to declare a class private method, is it better declare it static or not?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you understand what a static method really is?
 
Guido Granobles
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic