Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To restate the problem: you want your per-file-type classes to have statically available information on the type (e.g., name and description). </p> <p>We can easily get part-way there: create a separate class for your type info, and have a static instance of this (appropriately instantiated) in each per-file-type class.</p> <pre><code>package myFileAPI; public class TypeInfo { public final String name; public final String description; public TypeInfo(String name, String description) { this.name = name; this.description = description; } } </code></pre> <p>and, say:</p> <pre><code>package myFileAPI; public class TextFile { public static final TypeInfo typeInfo = new TypeInfo("Text", "Contains text."); } </code></pre> <p>Then you can do stuff like:</p> <pre><code>System.out.println(TextFile.typeInfo.name); </code></pre> <p>(Of course, you could also use getters in <code>TypeInfo</code> to encapsulate the underlying strings.)</p> <p>However, as you said, what we really want is to <em>enforce</em> the existence of a particular signature static method in all your per-file-type classes <em>at compile time</em>, but the 'obvious' design path leads to requiring an abstract static method in a common superclass which isn't allowed. </p> <p>We <em>can</em> enforce this <em>at run-time</em> though, which may be good enough to ensure it is coded correctly. We introduce a File superclass:</p> <pre><code>package myFileAPI; public abstract class File { public static TypeInfo getTypeInfo() { throw new IllegalStateException( "Type info hasn't been set up in the subclass"); } } </code></pre> <p>If <code>TextFile</code> now <code>extends File</code>, we will get this exception when calling <code>TextFile.getTypeInfo()</code> at runtime, unless TextFile has a same-signature method.</p> <p><strong>This is quite subtle</strong>: code with <code>TextFile.getTypeInfo()</code> in still compiles, even when there is no such method in TextFile. Even though static methods are bound at compile time, <em>the compiler can still look through the class hierarchy to determine the compile-time static call target</em>.</p> <p>So, we need code like:</p> <pre><code>package myFileAPI; public class TextFile extends File { private static final TypeInfo typeInfo = new TypeInfo("Text", "Contains text."); // Shadow the superclass static method public static TypeInfo getTypeInfo() { return typeInfo; } } </code></pre> <p>Note that we are still <strong>shadowing</strong> the superclass method, and so File.getTypeInfo() can still be 'meaninglessly' called.</p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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