Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class Method!

 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi folks,
what is the typical situation where static method in a class is required?
thanks.
 
Richard Jensen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and of course, the main method of a Java program must be static.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic