• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

about static method

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

im beginner in programming, could anyone please tell me. When to use Static method in java?
What purpose programmer use static method ?
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Consider a baby who has just been given a bath. The baby's mom is about to dress him up.
Before she can do that, at a minimum, she needs to have a towel to dry him up and clothes to dress him.
In Java speak, this would roughly translate to

As you can the dressBabyAfterBath can be called only if you have a Mom object reference. This is a non static method, meaning you need to have an object reference to invoke the method.

Now the baby is all dressed up and given to the Dad. Dad observes the baby and says "How are you cutie pie!". Notice he does not need any tools (like mom required the towel and clothes) to perform this action.
Again in Java speak this would roughly translate to

Notice the method inspectBaby is declared static and works perfectly.

As a rule of the thumb, when the method does not need to rely on any other variables or objects (e.g towel, clothes) you can safely define it as static.
 
Yuvaraj Deena
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for your valuable reply.
 
reply
    Bookmark Topic Watch Topic
  • New Topic