As per my understanding we should go for instance methods only when they are dealing with state of object i.e instance variable . If method does deal with state of object they should always be declared as class methods i.e static. But still in most of the projects
i ihave seen the methods which never not operates on instance variables they are also declared as instance methods(basically what these methods are doing they are using some of the method parametrs and doing some processing on that paremets and calling some other classes).Thats it.
Should not these methods should be declared as class method i.e static ?

4 Answers
4

It's likely the answer is yes: if you have an instance method that doesn't actually take advantage of the instance state, then it should probably be static, and possibly moved to a helper class depending on what it does.

Note that even if you don't access instance variables, accessing instance methods will also disqualify a method from becoming static. Also, if this method is an instance method in order to future-proof it (in anticipation of using the instance state later,) then changing it wouldn't be advisable either.

Also important is that public non-static methods could be inherited and overriden by a subclass, so making them static could actually break the code in possibly unexpected ways.

+1 If the methods are private static, this answer is probably correct. However, when it comes to public methods, it becomes an API question. If the method might ever become dependent on an instance field, or if you want to make it available as part of an interface, you suddenly have to go through your entire code-base to de-staticize all your method calls.
–
StriplingWarriorAug 26 '11 at 18:15

Here's a [possibly incomplete] list when you must use instance methods over static ones:

you access instance variables / methods from within the method

the method is an abstract method that you implement

the method is an interface method that you implement

you have doubts about the method staying static in the long-term

you declare it synchronized and don't want to lock on the class, rather on the instance

you get warnings when accessing static methods in a non-static way and you really care about them (sometimes you just can't avoid calling in a non-static way, so your only choice is making them methods non-static)

You cannot expect everyone to follow a path all the time whether it is best practice or not. First, we are all humans. We can choose a way over something different sometimes and that shouldn't be fully correct all the time. Even Frameworks and Libraries and Languages are created by humans so an error shouldn't surprise you or bedazzle you.