Conversation started Aug 26, 2018 at 10:57.
Aug 26, 2018 10:57
@Abcd I don't think wrapper class methods have to be static, though they can be. Any class can have static methods.
@JohnRennie What is the use of static methods? When to use them?
With an object typically that object has some data that is specific to the object. For example the URL object I mentioned earlier has a web page address that is specific to that object. I could have other URL objects that would have different addresses associated with them.
And in many cases the object methods won't work without having that data stored in the object.
But there might be some methods associated with a class that don't rely on having an object with some data in it. In that case there's no need to create an object to use that method because you don't need the info an object stores.
So you could make that sort of method static. Then you don't need to create an object to use it.
@JohnRennie Could you give a simple example for this?
Hmm ... let's see if I can think of an example ...
Suppose I have a vector object. I could have a method that set the x, y, and z components of the vector and I could have a method called Modulus that returned the length of the vector.
Obviously the Modulus method can't be static because it doesn't make any sense to call it unless we have a vector object with the x, y and z components set. OK so far?
@JohnRennie yes
Aug 26, 2018 11:05
But then I discover I might need to use both metric and imperial units, so I add a method to my object .ConvertMetresToInches(int Dist)
This method takes one argument, a distance in metres, and returns the distance converted to inches.
ok
this should be static, right?
The method doesn't need any of the data in the object, so I could make it a static method, then I wouldn't have to go to thetrouble of instantiating an object before I could use it.
Oh okay, got it! Thanks.
This is probably a slightly artificial example, but it should give you the general idea.
 
Conversation ended Aug 26, 2018 at 11:07.