When NOT to use the static keyword in Java?

后端 未结 9 772
轮回少年
轮回少年 2020-11-27 13:29

When is it considered poor practice to use the static keyword in Java on method signatures? If a method performs a function based upon some arguments, and does not require a

相关标签:
9条回答
  • 2020-11-27 14:17

    An additional annoyance about static methods: there is no easy way to pass a reference to such a function around without creating a wrapper class around it. E.g. - something like:

    FunctorInterface f = new FunctorInterface() { public int calc( int x) { return MyClass.calc( x); } };
    

    I hate this kind of java make-work. Maybe a later version of java will get delegates or a similar function pointer / procedural type mechanism?

    A minor gripe, but one more thing to not like about gratuitous static functions, er, methods.

    0 讨论(0)
  • 2020-11-27 14:22

    One reason why you may not want it to be static is to allow it to be overridden in a subclass. In other words, the behaviour may not depend on the data within the object, but on the exact type of the object. For example, you might have a general collection type, with an isReadOnly property which would return false in always-mutable collections, true in always-immutable collections, and depend on instance variables in others.

    However, this is quite rare in my experience - and should usually be explicitly specified for clarity. Normally I'd make a method which doesn't depend on any object state static.

    0 讨论(0)
  • 2020-11-27 14:25

    What you say is sort of true, but what happens when you want to override the behavior of that method in a derived class? If it's static, you can't do that.

    As an example, consider the following DAO type class:

    class CustomerDAO {
        public void CreateCustomer( Connection dbConn, Customer c ) {
           // Some implementation, created a prepared statement, inserts the customer record.
        }
    
        public Customer GetCustomerByID( Connection dbConn, int customerId ) {
           // Implementation
        }
    }
    

    Now, none of those methods require any "state". Everything they need is passed as parameters. So they COULD easily be static. Now the requirement comes along that you need to support a different database (lets say Oracle)

    Since those methods are not static, you could just create a new DAO class:

    class OracleCustomerDAO : CustomerDAO {
        public void CreateCustomer( Connection dbConn, Customer c ) {
            // Oracle specific implementation here.
        }
    
        public Customer GetCustomerByID( Connection dbConn, int customerId ) {
            // Oracle specific implementation here.
        }
    }
    

    This new class could now be used in place of the old one. If you are using dependancy injection, it might not even require a code change at all.

    But if we had made those methods static, that would make things much more complicated as we can't simply override the static methods in a new class.

    0 讨论(0)
提交回复
热议问题