Does it make sense to have a non static method which does not use an instance variable?

Well sure! Let’s assume that you have in interface IMyCollection. It has a method boolean isMutable().

Now you have two classes, class MyMutableList and class MyImmutableList, which both implement IMyCollection. Each of them would override the instance method isMutable(), with MyMutableList simply returning true and MyImmutableList returning false.

isMutable() in both classes is an instance method that (1) does not use instance variables, and (2) does not affect instance state. However, due to constraints in the language (it’s impossible to override static methods), this design is the only practical one.

Also, I would like to clear up a misconception (as @manouti did as well): non-static methods aren’t instance because they use any instance variables or affect instance state; they’re instance methods because they were defined that way (without the static keyword), and thus have an implicit this parameter (which, in languages like Python, is actually explicit!).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)