• 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

static methods

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q.What are the advantages/scenarios of defining a method as static.I am aware of some.
1)The method can be invoked without the need to create an Object first.
2)Calling from another static method.

Can anyone please suggest a few others.Also does it in any way affect performance/memory issues??

Thanks.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This topic is not part of the SCJP certification. I'm moving this to Java in General (Intermediate).
 
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
Static methods are (microscopically) faster, as they don't use a "this" parameter and there's no polymorphism; but it's the sort of small difference that's usually just swamped in the noise, and not something to worry about in general.

You can't say, overall, that static methods have any "advantages." Rather, they are appropriate in some situations, just as non-static methods are appropriate in others. The two reasons you gave are some of the important ones, but I think they both miss the big picture. It's not so much that static methods are "good" because you don't have to create an object to call them; it's that static methods are appropriate when a method will be called outside of the context of a particular object. These situations include methods that create objects ("factory" methods) and methods that process primitive values (all the methods in the java.lang.Math class.)
 
Screaming fools! It's nothing more than a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic