• 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

Static method in java

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All
I have doubt while using static method.In which case static method is preferred over other method.what qualities other then class method it has being static.Real time example explanation is highly appreciated.
Thanks
Praveen
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static Method ---> Class Level
Normal Method --> Instance Level.

Static method are being called by the class name. Basically this is needed when we want to achieve an objective in the class level. Whereas In Normal method , it can be called on the instance level
 
Ranch Hand
Posts: 874
Android VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Praveen,

I prefer static methods at cases when method functionality can be achieved by using the parameters sent to the method rather than the instance variables.

Say you want to append n zero's to int and convert to String . A simple and reusable solution would be a static method taking 2 int parameters ( one for the number of zeros to append , other the int value to convert as String)

 
Praveen Chaudhary
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Balu
I do agree with you,In your example If I am taking method without static that way also we can achieve the functionality so my question for what purpose exactly you make it static or what privileges it has being static or class method.

Balu Sadhasivam wrote:
Praveen,

I prefer static methods at cases when method functionality can be achieved by using the parameters sent to the method rather than the instance variables.

Say you want to append n zero's to int and convert to String . A simple and reusable solution would be a static method taking 2 int parameters ( one for the number of zeros to append , other the int value to convert as String)

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell's classification of methods
  • Methods which take information from the outside world. To be recognised by (type something) after their name.
  • Methods which don't take information from the outside world, to be recognised by () after their name.
  • Methods which return information to their caller to be recognised by a return type.
  • Methods which don't return information to their caller to be recognised by the keyword void
  • Methods which take or use information from their object or instance, to be recognised when you write the method.
  • Methods which alter information in their object, to be recognised when you write the method.
  • Methods which alter the state of their parameters, to be recognised by reading their documentation, or when you write the method.
  • When you put all that lot together, you get about 32 categories, but you will find some methods which make no use of information from their object, nor alter any information in their object. Those methods are candidates for being made static.
     
    Balu Sadhasivam
    Ranch Hand
    Posts: 874
    Android VI Editor Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Praveen,

    Yes you can achieve the same without static , but questions that you need to ask yourselves
    Are you using any instance specific variables inside the method ? Why do i need to create an object to access the such utility method ?
    Take Math class , Does it make sense to create Math object , just to find minimum of 2 ints or say ceil of double ? really no.. these utility methods can exists without being called in OO context.
     
    Ranch Hand
    Posts: 37
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This example may help. You should use static methods when you don't need any information from the current instance of an object in order to complete the work. Utility or helper classes are a prime example of when to use static methods. Take a look at the Person class below. getAge() uses information from the instance in order to calculate the age of a person. The calculateAge() method takes a date argument (date of birth) and uses that date to calculate the age for any date that is passed in, and no reference to an instance of person is needed.

     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic