Note that there are some explanatory texts on larger screens.

plurals
  1. POEmulating extensible enums using interfaces
    primarykey
    data
    text
    <p>So here I was trying this "Effective Java 2nd edition" exercise about writing extensible enums. </p> <p>I faced a really strange issue though - I had each of the enum instances implement the interface methods and all looked good in eclipse. But when I tried to compile it through maven, it failed, although eclipse didn't throw any compilation errors previously at all (both my eclipse and maven use the same JDK: <code>1.6.0_33-b05</code>). It turned out that I had to override the interface method(s) [I'll call it <strong>method X</strong> from now] inside the enum (outside of all enum instances) too for fixing this!</p> <p>Here's a sample explaining this:<br/></p> <p><strong>Interface:</strong></p> <pre><code>public interface IDay { public boolean isHoliday(); } </code></pre> <p><strong>Enum:</strong></p> <pre><code>public enum Day implements IDay { SATURDAY { @Override public boolean isHoliday() { return true; } }, SUNDAY { @Override public boolean isHoliday() { return true; } }, MONDAY { @Override public boolean isHoliday() { return false; } }; // Method X: the project won't compile for me without this method! public boolean isHoliday() { return false; } } </code></pre> <p><strong>Invocation:</strong></p> <pre><code>public class DayTester { public static void main(final String[] args) { // line Y: this line won't maven compile if I got rid of the method X System.out.println("Sunday: " + Day.SUNDAY.isHoliday()); } } </code></pre> <p>Strange enough, eclipse was completely ok without the method <strong>'X'</strong> yet the maven compilation would fail on the line Y saying </p> <blockquote> <p>[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project X: Compilation failure [ERROR] DayTester.java:[7,46] cannot find symbol [ERROR] symbol : method isHoliday() [ERROR] location: class Day</p> </blockquote> <p>And my eclipse save actions automatically inserts @Override if I had the method X in place. Removing the method X throws compilation errors in my eclipse aon those Override annotations I had previously. </p> <p>Here are my questions: 1. Why won't maven compile in this case when eclipse does? 2. What do the Override annotation errors mean here? 3. Is the method X even accessible somehow? What am I missing to understand? </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.
 

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