This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Class Method! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class Method!" Watch "Class Method!" New topic
Author

Class Method!

Brian Smith
Ranch Hand

Joined: Oct 26, 2002
Posts: 232
hi folks,
what is the typical situation where static method in a class is required?
thanks.
Richard Jensen
Ranch Hand

Joined: May 14, 2003
Posts: 67
Remember that the difference between a static and non-static methods is whether there is an instance (object) of that class as an implicit parameter to the method. Static methods are often used to produce instances, or for algorithms that don't depend on having an object. (Math comes to mind).


Richard
N 37 33 W 122 18
Francis Siu
Ranch Hand

Joined: Jan 04, 2003
Posts: 867
hi Namaste Sathi
what is the typical situation where static method in a class is required?
The static method can be used to return the static variable.

It can also be used to share the process of operation to other objects(remember that it is not the individual instance method) I think that use the class method can reduce more memory space than using instance method if many object created.(It is related to performance)
My knowledge is limit in the performance,but Jim knows more about the knowledge of performance


Francis Siu
SCJP, MCDBA
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Static methods get into some controversial areas in OO discussions. One thing that distinguishes OO from procedural code is that an object encapsulates behavior plus state. A lot of static methods have no state. Integer.parseInt has an input parameter and output return value, but does not deal with any object state. It can be argued (I'm not takin sides here!) that when you have no state, you have a poor object. Some OO theorists are happy to catalog these as "function" objects or some such thing.
Some static methods do have static state. Caches are a good example - you only want one copy of the cache so static works. Others could go either way, but using static methods saves the client the effort of creating an instance. Frinstance I made a logger with static methods so you could do:

instead of

And some could argue that I was just being lazy. And this prevents me from having two loggers with different configurations at the same time.
One can also make a strong argument that static data is just poorly disguised global data, with all the problems that come with globals.
Hope that helped.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
...and of course, the main method of a Java program must be static.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
 
subject: Class Method!
 
Similar Threads
How to call a stand alone class from WAS???
java.lang.OutOfMemoryError: PermGen space
Object
Events
deprecated methods