Re: Pros and Cons of Static Methods

From: Chris (sarge_chris_at_hotmail.com)
Date: 05/27/04

  • Next message: Ryan Stewart: "Re: Pros and Cons of Static Methods"
    Date: 27 May 2004 04:04:18 -0700
    
    

    > It just goes against what I've been taught about OO. For example a static
    > method cannot be overridden... correct? That seems to go against
    > polymorphism. Exposing multiple static methods in place of constructors
    > seems a bit unOO if you will to me.

    You've picked on a classic issue that I personally have never found an
    answer to one way or the other.

    As an example - which of these is best (using your example):

    class Employee
    {
        ...
        public static Collection getAllEmployees() { ... }
    }

    Collection allEmployees = Employee.getAllEmployees();

    or:

    class Employee { ... }

    class EmployeeMaintenance // or some such name
    {
        public Collection getAllEmployees() { ... }
    }

    EmployeeMaintenance em = new EmployeeMaintenance();
    Collection allEmployees = em.getAllEmployees();

    Hard to say really IMHO.

    If there was just one or two static methods on a class then I'd say
    the first because it's easier to maintain. If the 'maintenance' class
    expanded to many methods (searches, CRUD stuff, etc) then a separate
    class is probably better for cohesion reasons.

    As to whether static methods are not OO or not, I don't know, it's
    probably only of interest to academics.

    - sarge


  • Next message: Ryan Stewart: "Re: Pros and Cons of Static Methods"