Search Java Programs

Tuesday, February 2, 2010

Why we really need a static method?

Questions

What is the use of static method? Is this to give access only to static members or any other reason behind this? Even it is not a static method it ll be shared by all objects of particular class. Why we really need a  static method?

Answers

This is a difficult question, when you start to use a language, when the reason for a language design is asked for. Static methods do not need an instance. e.g the Math Class has a lot of static methods. The processing in this methods do not need an instance.

Everything can be done in this one method with Math. (), otherwise you have to create an instance when you invoke such a method.

The main-Method is a static method. At the start you do not have an instance.

The factory design pattern/singletons use static methods. ( they often do not have public constructors to prevent creating a direct instance).

HTH
Ewald


******************************************************************************************************************


Hi all,
Suppose if we need to make a class whose methods(to be defined bye the user) need to be used in the development then we need to make it static so that no object can have a separate copy of the method and it can simultaneously be used as a template methods like

Math.square();

Nishant Bhushan


******************************************************************************************************************


Hi,

This answer really makes me clear. Thanks for your reply.

Regards,
Suren.

No comments:

Post a Comment

Website Design by Mayuri Multimedia