This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static methods

 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • 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
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • 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.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • 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.
 
James Redpath
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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?
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic