While reading a book, i came across this statement:
The methods of an object define its behaviour. These methods are called instance methods. It is important to note that these methods pertain to each object of the class. This should not be confused with the implementation of the methods which is shared by all instances of the class.
I know what instance methods are, I know what static methods are (i.e. shared by all instances of the class). The above statement seem to suggest that the implementation of the methods is shared by all instances. What exactly does this mean? It doesnt sound like it is refering to static methods.
It means that you don’t get an extra copy of the code itself alongside each instance of the class. The behaviour is associated with an instance of the class, so it has that context, but there’s no extra “per instance” price to pay in terms of memory etc for instance methods.
One quick point to note about
static: I don’t like the description of it being “shared by all instances of the class” as that suggests that if there aren’t any instances, it’s unusable. I prefer to say that it’s associated with the class itself instead of a specific instance of the class.