• Post Reply Bookmark Topic Watch Topic
  • New Topic

about static method  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you so much for your valuable reply.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!