• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Using static methods

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 8528
615
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you got right. Welcome to the Ranch
 
Marshal
Posts: 76867
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Maybe he went home and went to bed. And took this tiny ad with him:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic