1) Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when there is no data or behavior in the class that depends on object identity.
What does it mean by functions do not change regardless of what happens to the object? How can a function change? Static variables, yes, but functions?
What does “object identity” mean? Would that be for example, a specific house in a house object? (So we deal with a specifically chosen house). So a static class would not work on any house objects?
2) If I don’t work with instance state, should I always use static classes? What’s the difference between a static and instance class working with instance state? All I can think of is the static class will only be of 1 instance and thus 1 instance means many calls will be queued if the class worked with instance state (if there is thread synchronisation)? Obviously, static state should only need a static class.
Is this correct?
Answer to question 1)
Imagine that you have a Car object with a field called speed. If you create a new car object and then set the speed property by using this function
the function SetSpeed would depend on the car object being initialized so it could access the speed property. Hence, without an initialized car SetSpeed would not be invokable. Had the function, however, been static, then it would not have access to any specific initialized car object, only other static defined objects.
Answer to question 2)
A static function does not have access to any instance declared functions or variables, so having a static function work with instance state items does not make sense.