• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

To be static or not to be static

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why and when should we declare methods as static in a class.
Are there any performance gains? and does it effect in multithreading environment
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Methods are chosen to be declared as static when they perform some tasks,which are not specific to any particular object of the class.
as static methods belong to the class, they can be accessed by more than one thread at a time.and one has to be careful in programming. There is every chance that what ever content on which this methods works may be inconsistant.
 
Monmohan Singh
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but do they improve performance in any way?
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, they do. If you do not need an instance of an object to call the method, then why create the object? Object creation is inherantly an expensive task.
Secondly, static methods do no participate in overriding/polymorphism. There is no need to maintain/use a dynamic look-up table to determine which version of the method to call. In fact, the method to call is determined at compile-time by the compiler.
However, don't let performance guide you in a quest to make all methods static. Stick to good design, using static methods only as needed, and look for performance optimizations other places (DB access, Network I/O, etc).
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also keep in mind that static methods don't have access to any of the instance variables. So if you need manipulate instance variables you CAN'T use a static method. As Joel pointed out, the design should dictate the usage.
 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic