Results 1 to 5 of 5
  1. #1
    ajincoep is offline Member
    Join Date
    Aug 2010
    Posts
    10
    Rep Power
    0

    Default Why to use static methods

    Hi
    I am newbie to Java, I was wondering why do we use static methods.
    Thanks

  2. #2
    ajincoep is offline Member
    Join Date
    Aug 2010
    Posts
    10
    Rep Power
    0

    Default

    I mean to say in which screnario do we use static methods.
    It would be good if you give some real time example

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    One of the best reasons is to create a factory class--that is, a class that creates objects of other types.
    Java Code:
    public class CarFactory {
        public static Car create() {
            Car n = new Car();
            n.setModel("some_model.3ds");
            n.setWheels(WHEELS_SPINNING);
        }
    }
    Now, we can just call CarFactory.create() instead of having that code every time we want a new object.

    This being said, you can make any method object-oriented instead. Instead of this being static, you could declare a new CarFactory object, then have your carFactoryObj.create() method called.

    Usually static methods are used to do something globally. Perhaps you have a timer class that manages all time in the application. When you have 50 classes, it's hard to keep track of your timer. Perhaps it's best just to do timeManager.startTimer(), rather than have to store a timeManager object and call it each time. It can save memory in this fashion as well.

    It all comes down to personal preference. In my current project, I have several static methods in my main Applet class, which serve as debugging commands and references to objects. For example:
    Java Code:
    	public static void Pause() { toc.s_ShouldThink = false; }
    	public static void Resume() { toc.s_ShouldThink = true; }
    	public static tocCConnection Client() { return toc.s_NetManager.m_Client; }
    	public static tocSConnection Server() { return toc.s_NetManager.m_Server; }
    ...etcetera. Corresponding to these are many static variables:
    Java Code:
    	public final static boolean s_Debug = true;
    	public static Color s_Black = new Color(0,0,0);
    	public static Color s_White = new Color(255,255,255);
    ...and so on.

    Hope that helps a bit!

    PS: Notice that in any snippets I pasted, s_ indicates a static variable while m_ indicates a class member variable.

    PPS: Did a bit of Googling for you (though I shouldn't, should let you do it instead), and here are a few other explanations & points of view.
    http://www.javaworld.com/javaworld/j...py.html?page=1
    http://forums.sun.com/thread.jspa?threadID=562350
    http://bytes.com/topic/c-sharp/answe...-static-method

    Good luck! :)
    Last edited by Zack; 08-03-2010 at 08:30 AM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Look at the Math class and see if you can figure out why making that a "normal" class that requires objects to be instantiated is a bit pointless.

    Otherwise known as utility classes...

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    While I agree with you, Tolls, in that utility classes are often static (as is my custom math class--Pi(), Rand(), etc), some are not. A custom array class or a string manipulation class, which would both be utilities, would (likely) have a local variable and non-static method access.

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. static methods
    By billq in forum New To Java
    Replies: 3
    Last Post: 12-30-2009, 02:34 PM
  3. Recursion with static and non static methods
    By sh4dyPT in forum New To Java
    Replies: 14
    Last Post: 03-27-2009, 06:56 AM
  4. Static methods
    By carderne in forum New To Java
    Replies: 10
    Last Post: 01-03-2009, 05:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •