Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java static methods required implementation

Tags:

java

static

The task I had to make has already been delivered, yet a certain question hangs around in my mind.

I defined the following interface:

package dao;

import java.sql.SQLException;

/**
 * Get / save / delete a single instance from the db in RESTful fashion on the object
 * @author kimg
 *
 * @param <T>
 */
public interface IDao<T> {

    public void fetch(int id); 
    public void save() throws SQLException; 
    public void delete() throws SQLException; 

} 

Purpose is to have all pojo's that are represented as database table entities implement these methods, so the user of the pojo's knows how to handle instances according to a pattern. I've taken this approach from Backbone (Javascript).

However, there are other methods that I liked to impose as class methods (static) on the Pojo class itself. The most obvious methods are things like:

List<Person> list = Person.fetchAll(); 
List<Person> list = Person.fetchAll(offset, limit); 
int i = Person.countAll(); 
... 

I've found that having these methods defined by default offers great benefit, yet nothing forces a developer to implement them, as static methods can't be imposed by default. Also, users of the classes can't know for sure that they'll be able to use the static methods that they would otherwise expect by contract (in case of an interface); any typo in the method name, omitting any method can cause the smooth app workflow to break.

What would be the most elegant solution to catch this pitfall?

like image 984
html_programmer Avatar asked Feb 08 '16 18:02

html_programmer


People also ask

How are static methods implemented in Java?

Syntax to declare the static method:Access_modifier static void methodName() { // Method body. } The name of the class can be used to invoke or access static methods.

How are static methods implemented in class?

Similar to Default Method in Interface, the static method in an interface can be defined in the interface, but cannot be overridden in Implementation Classes. To use a static method, Interface name should be instantiated with it, as it is a part of the Interface only.

Do static methods need to be instantiated?

In the same way that a static variable exists before an object of the class is instantiated, a static method can be called before instantiating an object. So when a method doesn't need to access any stored values, a static method is appropriate.

Why static methods are introduced in Java 8?

Java interface static method helps us in providing security by not allowing implementation classes to override them. We can't define interface static method for Object class methods, we will get compiler error as “This static method cannot hide the instance method from Object”.


1 Answers

I've found that having these methods defined by default offers great benefit, yet nothing forces a developer to implement them, as static methods can't be imposed by default.

If the methods are so beneficial then wouldn't developers want to implement them? At least if they were aware of the desired patterns and the usefulness of implementing them?

Also, users of the classes can't know for sure that they'll be able to use the static methods that they would otherwise expect by contract (in case of an interface)

That's irrelevant, because static methods are not virtual. Users do know for sure whether the type whose static method they would like to invoke in fact does have that method. If they forget then the compiler will gladly tell them.

any typo in the method name, omitting any method can cause the smooth app workflow to break.

I'm not sure I follow you. If you're talking about developers failing to implement these static methods or doing so wrongly, then that's what code review is for. You could also write automated tests that reflectively verify the methods' presence. I don't really see how this is a bigger issue than the methods being implemented with the desired signature but incorrect implementation.

Again, static methods are not virtual. The user of every static method call knows at compile time exactly which method is being called, and therefore whether such a method is available. If they want a particular method and do not have it then they have the alternatives of requesting that it be added, adding it themselves, or doing without.

What would be the most elegant solution to catch this pitfall?

Document any such expectations. Have project management enforce them (and if they refuse to do, then maybe it wasn't so important after all). Employ code reviews. Write automated tests, perhaps reflective ones.

like image 51
John Bollinger Avatar answered Sep 30 '22 16:09

John Bollinger