File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static Method in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static Method in java" Watch "Static Method in java" New topic
Author

Static Method in java

sakthi moorthy
Ranch Hand

Joined: Oct 17, 2007
Posts: 54
hai to all my doubt is what is the purpose of the static method in java class when there is no main method like valueOf() method in String class
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
As you probably already know, a static method is one that can be invoked without having to first instantiate the containing class. In my experience, static methods are most often used for one of these two purposes:
  • Utility methods that provide some general functionality that's not specific to any one object. Example: Math.abs()
  • Factory methods to perform indirect instantiation of a class. Example: String.valueOf()

  • Some developers also use static methods as a substitute for the Singleton design pattern to enforce that only one "instance" of the class can ever exist at any given time. This is arguably a bad practice, however.


    SCJP 5.0
    Burkhard Hassel
    Ranch Hand

    Joined: Aug 25, 2006
    Posts: 1274
    A static method is a method for a class, not for individual objects. It behaves the same for all individual objects. However it is allowed but not recommended (to avoid confusion) to call a static method on an object.
    The classical example of a static method (besides main ) is an object counter, e.g.

    The last two lines compile nicely but this is not recommended.

    Output:
    0
    2
    2
    2



    Yours,
    Bu.


    all events occur in real time
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Static Method in java