質問

I have read many posts and articles and now understood that static methods can not be inherited. To explain my issue, you can look at the following pieces of code:

The main class:

public class DataTable {
public String id;
public String date;

public String reserve() throws SQLException {
    ......
    String query="insert into `" + this.getClass().getSimpleName() +"`() values ()";
    ........
    String key = ......
    .......
    return key;
}
}

One of the subclasses:

public class Contact extends DataTable{
public String firstName;
public String lastName;
public String phoneNumber;
    ...........
}

With this, if I need at some point of time to reserve a contact, I would need to write:

Contact contact=new Contact();
String id = contact.reserve();

This works. But I find it is not optimal. To me ,the first line is completely useless. I would much prefer writing:

String id = Contact.reserve();

The code would be cleaner and I would guess that it would use less resources at runtime, as it wouldn't have to create an instance of Contact which we really don't need. But for that, I should either:

  • Create a specific reserve() static method in each subclass and remove it from the main class. I really don't think it is any better, as I would have the exact same piece of code in a big number of classes;
  • or make reserve() static in the main class, and allow it to access the classname from which it was called, which does not seem feasible in java.

I hope my question is clear enough. Any ideas on how to handle that? Or should I just keep it as it is?

役に立ちましたか?

解決

Don't mistake fewer lines of code for "cleaner".

The code would be cleaner and I would guess that it would use less resources at runtime, as it wouldn't have to create an instance of Contact which we really don't need.

I disagree. You're clearly reserving that contact object, not the class, so calling reserve on an instance of that class makes much more sense. Of course, if you want to narrow this down to one line you could do:

new Contact().reserve();

...which may or may not be clearer depending on if you need to reuse the contact object. Don't get caught up in the overhead of creating objects in cases like this - good style is much more important. Unless you notice that things aren't fast enough that way, then you haven't got a problem.

In short, just from that above code, it sounds to me like things are perfectly fine the way they are.

他のヒント

If it's all about redundant lines of code then what's wrong with:

String id = new Contact().reserve();

Of course that is going to create a Contact object each call. If that is not acceptable then why not use a singleton:

String id = Contact.instance.reserve();

This will only ever create a single instance of Contact and has the advantages of reserve not being static.

public class DataTable {
    //...

    public static String reserve(Class clazz) throws SQLException {    
        ......
        String query="insert into `" + clazz.getSimpleName() +"`() values ()";
        ........
        String key = ......
        .......
        return key;
    }
}

then you can call it like this DataTable.reserve(Contact.class);

Your Contact already extends DataTable so you already have a reserve method at Contact. You can now override that method or even write new methods

public class Contact extends DataTable {

   public String reserve() throws SQLException {
       super.reserve();
       System.out.println("I override the super class");
   }

}

If you do not extend DataTable then you essentially treat it as a utility so your DataTable has a signature similar to

public class DataTable {
     public static String doReserve(String id, Date date) {
         // do your logic
     }
}

and simply call the DataTable.doReserve method statically from Contact.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top