As my name suggests, I am a .NET developer but I have a growing interest in Java, and I am interested in learning more about other languages as this helps me to learn more about programming generally.
Anyway, my question is this: Methods which don’t take parameters/work with state (which is just parameters in the method, correct me if I am wrong) are recommended to be made static. What is the relationship/link between static and parameterless methods? Not working with state means if you pass a Person object into the method, and you don’t edit that object’s state (Eg its properties) – this is my understanding.
I don’t mind any Java specific answers.
Thanks
There is no relationship between static and parameterless methods.
A static method is one which does not access instance state in the receiving class (and therefore does not need to be associated with a particular instance). It can easily take parameters:
A static method can access its parameters (and can therefore modify their state if they allow it):
Conversely, a parameterless method might well need to access receiver state, and therefore be an instance (non-static) method:
(I’ve posted code samples in C# because this is language-independent; the same notions and distinctions apply in Java, possibly with a few keyword changes.)