aspose file tools*
The moose likes Java in General and the fly likes Static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Static methods" Watch "Static methods" New topic
Author

Static methods

Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
hi all:
When one should choose a method to be static? Does static method effect performance in a multi-threaded environment?
thanks


SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
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.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
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

Joined: Sep 29, 2002
Posts: 1683
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.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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.


Tony Morris
Java Q&A (FAQ, Trivia)
James Redpath
Greenhorn

Joined: Aug 02, 2003
Posts: 13
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

Joined: Oct 12, 2000
Posts: 5093
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.


42
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static methods