Note that there are some explanatory texts on larger screens.

plurals
  1. POSimulate static abstract and dynamic linking on static method call in Java
    primarykey
    data
    text
    <h2>Introduction</h2> <p>As a disclaimer, I'v read <a href="https://stackoverflow.com/questions/370962/why-cant-static-methods-be-abstract-in-java">Why can&#39;t static methods be abstract in Java</a> and, even if I respectfully disagree with the accepted answer about a "logical contradiction", I don't want any answer about the usefulness of <code>static abstract</code> just an answer to my question ;)</p> <p>I have a class hierarchy representing some tables from a database. Each class inherits the <code>Entity</code> class which contains a lot of utility methods for accessing the database, creating queries, escaping characters, etc.</p> <p>Each instance of a class is a row from the database.</p> <h2>The problem</h2> <p>Now, in order to factorize as much code as possible, I want to add information about related columns and table name for each class. These informations must be accessible without a class instance and will be used in <code>Entity</code> to build queries among other things.</p> <p>The obvious way to store these data are static fields returned by static methods in each class. Problem is you can't force the class to implement these static methods and you can't do dynamic linking on static methods call in Java.</p> <h2>My Solutions</h2> <ol> <li>Use a <code>HashMap</code>, or any similar data structure, to hold the informations. Problem : if informations are missing error will be at runtime not compile time.</li> <li>Use a parallel class hierarchy for the utility function where each corresponding class can be instantiated and dynamic linking used. Problem : code heavy, runtime error if the class don't exist</li> </ol> <h2>The question</h2> <p>How will you cope with the absence of <code>abstract static</code> and dynamic linking on abstract method ?</p> <p>In a perfect world, the given solution should generate a compile error if the informations for a class are missing and data should be easily accessible from withing the Entity class.</p> <p>The answer doesn't need to be in Java, C# is also ok and any insight on how to do this without some specific code in any language will be welcomed.</p> <p>Just to be clear, I don't have any requirement at all besides simplicity. Nothing have to be static. I only want to retrieve table and columns name from <code>Entity</code> to build a query.</p> <h2>Some code</h2> <pre><code>class Entity { public static function afunction(Class clazz) { // this parameter is an option // here I need to have access to table name of any children of Entity } } class A extends Entity { static String table = "a"; } class B extends Entity { static String table = "b"; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload