Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I agree - I feel that this is a limitation of Java. Sure, they have made their case about the advantages of not allowing inherited static methods, so I get it, but the fact is I have run into cases where this would be useful. Consider this case:</p> <p>I have a parent <code>Condition</code> class, and for each of its sub-classes, I want a <code>getName()</code> method that states the class' name. The name of the sub-class will not be the Java's class name, but will be some lower-case text string used for JSON purposes on a web front end. The <code>getName()</code> method will not change per instance, so it is safe to make it static. However, some of the sub-classes of the <code>Condition</code> class will not be allowed to have no-argument constructors - some of them I will need to require that some parameters are defined at instantiation.</p> <p>I use the <code>Reflections</code> library to get all classes in a package at runtime. Now, I want a list of all the names of each <code>Condition</code> class that is in this package, so I can return it to a web front end for JavaScript parsing. I would go through the effort of just instantiating each class, but as I said, they do not all have no-argument constructors. I have designed the constructors of the sub-classes to throw an <code>IllegalArgumentException</code> if some of the parameters are not correctly defined, so I cannot merely pass in null arguments. This is why I want the <code>getName()</code> method to be static, but required for all sub-classes.</p> <p>My current workaround is to do the following: In the <code>Condition</code> class (which is abstract), I have defined a method:</p> <pre><code>public String getName () { throw new IllegalArugmentException ("Child class did not declare an overridden getName() method using a static getConditionName() method. This must be done in order for the class to be registerred with Condition.getAllConditions()"); } </code></pre> <p>So in each sub-class, I simply define:</p> <pre><code>@Override public String getName () { return getConditionName (); } </code></pre> <p>And then I define a static <code>getConditionName()</code> method for each. This is not quite "forcing" each sub-class to do so, but I do it in a way where if getName() is ever inadvertently called, the programmer is instructed how to fix the problem.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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