• 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:

Static methods

 
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all:
When one should choose a method to be static? Does static method effect performance in a multi-threaded environment?
thanks
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hanna,
This isn't really an advanced issue. Static methods are used when a method is more applicable to the class object rather than instances. If you're having a hard time wrapping your head around "static" I'd recommend a basic programming book. As for the second part of your question, since static just has to do with class versus instance scope, it doesn't really make any sense. There may be some performance differences when one is used over the other, but performance shouldn't really be used as an issue when deciding whether to implement something as a static member versus an instance member.
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stoddard,
"This isn't really an advanced issue"
Maybe it is advanced for me.
"Static methods are used when a method is more applicable to the class object rather than instances"
we all know that..!!
"There may be some performance differences when one is used over the other"
What are the performances differnces, if you know share it.
"but performance shouldn't really be used as an issue when deciding whether to implement something as a static member versus an instance member"
No one mention that performance will be used as an issue.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One reason to use a static method is when there is no need to instantiate the class. For instance, think of utility methods: do you need an instance to run a method which will do some calculation and return the result? If you look at the Math class, all the methods are static. The Math class just provides a namespace for these methods.
Incidentally, a class which has only static members should have a private constructor to prevent pointless instantiation.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fundamental OO concepts are not to be considered advanced by definition (i.e. it is easily argued that "fundamental" is a contradictory adjective of "advanced").
http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html
Good luck.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at Sun Bug document 4283424 (http://online.sunsolve.sun.co.uk) for Allocation, static method performance issues uncovered; when you have multiple CPUs on a machine and use static methods in threads there is a problem but this only says something about Java 1.2. What about 1.4?

Does anybody know about Java 1.4?
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Hanna Habashy:
Stoddard,
"This isn't really an advanced issue"
Maybe it is advanced for me.



If it is you should not call yourself SCJD and SCJP.
Such concepts are so basic you will need considerably more than them to pass those exams.
 
And will you succeed? Yes you will indeed! (98 and 3/4 % guaranteed) - Seuss. tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic