Results 1 to 8 of 8
  1. #1
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default Abstract Class with Static Methods

    I am in need of an advice. I am designing my application and want to know when to use abstract class with static methods?

    Abstract calls cannot be instantiated. If I don't declare the methods in Abstract class static, how can I call them?

    Thanks.
    --------

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    9

    Default

    Quote Originally Posted by bugger View Post
    I am in need of an advice. I am designing my application and want to know when to use abstract class with static methods?

    Abstract calls cannot be instantiated. If I don't declare the methods in Abstract class static, how can I call them?

    Thanks.
    --------
    If I understand your question correctly, you simply need to extend the abstract class.

    Java Code:
    public abstract class AbstractExample {
      public static void foo() {
        System.out.println("Static foo called.");
      }
      
      public void bar() {
        System.out.println("Non-static bar called.");
      }  
    }
    Java Code:
    public class AbstractCaller extends AbstractExample {
      public static void main(String[] args) {
        AbstractCaller ac = new AbstractCaller();
        ac.foo();
        ac.bar();
      }      
    }
    Output:
    Static foo called.
    Non-static bar called.

    Hope this helps, see this Sun topic for more info.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  3. #3
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Thanks.
    But you are calling both methods (foo and bar) with the object (ac). What role the static method played here?

  4. #4
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    8

    Default Static methods

    Hello bugger

    CaptainMorgan's example explains how to call the static and non-static methods and not why someone would like to do that. Static methods can only access static attributes of a instanced class. For example:
    You have a class called Dog with a static field called numberOfDogs. In the constructor of Dog you can increment numberOfDogs and if you override the finalize method then you can decrement numberOfDogs. This way you can track how many Dog objects that you have created thanks to the static modifier. Now, you can add static methods to the Dog class to manipulate numberOfDogs without you having to create an instance of Dog

    Hope this helped. ;)
    Last edited by tim; 01-04-2008 at 02:03 PM.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  5. #5
    bugger is offline Senior Member
    Join Date
    Nov 2007
    Posts
    111
    Rep Power
    0

    Default

    Thanks tim.
    Got it.

  6. #6
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Default Static Methods

    I think that all of the above posts right.
    But you guys are probably too experienced to answer the questions guys like me may ask (i am new to Java).

    Two things here to understand : First of all, Abstract Class cannot be instantiated. And Second - you don't have to Instantiate class (including abstract by extending and etc) in order to call Static Method. SO STATIC METHOD IS OUR FRIEND HERE!!!!

    Imagine this : You have Class Animal and you may want to have it Abstract. Then You have Static variable like Count of your Animals, and Static Method GetCount() to keep track of it. So you can call directly Animal.GetCount() without even extending our abstract class.


    To CaptainMorgan : You sir right there, but the difference between static and non static is explicitely exposed below, I have changed your code slightly :

    Code:
    public abstract class AbstractExample {
    public static void foo() {
    System.out.println("Static foo called.");
    }

    public void bar() {
    System.out.println("Non-static bar called.");
    }
    }Code:
    public class AbstractCaller extends AbstractExample {
    public static void main(String[] args) {
    AbstractExample.foo(); \\ this is where we get our AnimalCount for exmple
    AbstractCaller ac = new AbstractCaller();
    \\ac.foo(); - NO NEED TO HAVE ac IF WE WANT TO CALL foo();
    ac.bar();
    }
    }

  7. #7
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    True that, but:

    1. Everything static just looks ugly. :P
    2. Static methods/vars have limitations and disadvantanges you sometimes don't want.

    Usually only the things that really need to be static are static, not the opposite.

    I suggest: never make things static, unless you have no choice.
    I die a little on the inside...
    Every time I get shot.

  8. #8
    Join Date
    Sep 2008
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Supamagier View Post
    True that, but:

    1. Everything static just looks ugly. :P
    2. Static methods/vars have limitations and disadvantanges you sometimes don't want.

    Usually only the things that really need to be static are static, not the opposite.

    I suggest: never make things static, unless you have no choice.
    What's Static is Static sir. What is not Static is not Static, you can't replace one thing with another.

    In simple words, for instance
    Total Count of Items is Static, you cant make it any other type.
    Color of the item can not be static, since it is different from instance to instance.

    I am sorry but its not something redundant. Static type there for a reason.

Similar Threads

  1. Final,abstract and static
    By $hr!k@nt in forum Advanced Java
    Replies: 5
    Last Post: 01-09-2008, 06:23 AM
  2. Mocking static methods of class
    By Kat in forum New To Java
    Replies: 3
    Last Post: 11-08-2007, 01:24 AM
  3. Static methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-04-2007, 06:56 PM
  4. question about abstract methods
    By osval in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:36 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
  •