When Should we use static methods in JAVA?

Love and Share

Answer:  Define static methods are usually used in the following scenarios:

– If you are writing utility classes and they are not supposed to be changed.

– If a method doesn’t modify state of object, or not using any instance variables.

– If the method does not use any fields (or only static fields) of a class.

– If any operation is not dependent on instance creation.

 – If there is some code that can easily be shared by all the instance methods, extract that code into a static method.

– It is also used, If the method is not using any instance variable.

 –  Static methods can not be overridden. So use it, If you are sure that the definition of the method will never be changed or overridden.

 – If function of method will remain static across class hierarchy

Love and Share

Leave a Reply

Your email address will not be published. Required fields are marked *