• Post Reply Bookmark Topic Watch Topic
  • New Topic

when to use static methods  RSS feed

 
Vishal Dhingreja
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

This was one of the interview question I had to face.
In a practical scenario what are the parameters on the basis of which we decide to have our methods as static & when not.

What would be a logical answer to having inner classes rather than normal classes

Rgrds
Vishal D
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say you'd only use static method when you need something like a global utility function. The Math class is a good example of this - its just a set of calculation algorithms. Creating instances of objects all over the place just to use a method on the object is possibly not a great idea, particularaly as you don't care about what data the object could hold, just that it has the ability to give you a cosine value fro example. However, if you find yourself using lots of static methods its a good indication that your design could be better.
 
john guthrie
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
random thoughts...

1) obviously, you don't use static if you need more than one of something with different states.

2) one problem with making methods static is that they cannot be overridden, so if you envision any sort of class hierarchy, you don't want to be creating static methods
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!