• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using static methods  RSS feed

 
Aron Silvester
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just wanted to get your opinion on my statement about static methods to see if I have a good grasp on it.

- Static methods belong to the class and not a specific object of that class. Static methods are convenient to use because they can be called directly from the class whenever you need them. It is appropriate to use static methods when you know that a function of the method will not change throughout the class hierarchy or across instances. An example would be a class named Metric that converts distance in miles to kilometers. In the Metric class you have two defined static methods, milesToKolometers and kilometersToMiles, both returns a converted value in their respected units. In this class there is no need to collect and store any data. The class's sole purpose is to convert miles to kolometers and vice versa. Thus it would be reasonable/appropriate to make those methods static.

Does that sound right to you? Would you add anything else to that statement?
 
Liutauras Vilda
Marshal
Posts: 4657
319
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you got right. Welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never ask about static methods when I am around otherwise I shall link to the most dubious classification of methods known to modern science.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!