Java 8 upgrade leads to compiler errors and inherited static enumerations
We are upgrading the Java 6 project to Java 8 Recompiling using java 8 will be in Java awt. An error occurred in the frame subclass. I have simplified it as follows:
Organization / example / foo java
package org.example; import org.example.Type; import java.awt.Frame; public class Foo extends Frame { public Foo() { System.out.println(Type.BAZ); // <=== error here,BAZ cannot be resolved } }
Organization / example / type java
package org.example; public class Type { public static final int BAZ = 1; }
What seems to happen is that even if there is an org example. Type, static enumeration introduced in Java 7 awt. Window. Type is also preferred Is it correct?
Does that mean we have to use org example. Type fully qualifies all references to type?
Solution
Yes The type class is new, but the behavior is not This is a novice to design, not Java 8
Yes, as long as you extend a class that contains type members
I'll question why you extend the frame: most people extend the frame or JFrame, they shouldn't be Conducive to the formation of heritage, all this
Another approach might be to use static import to import type members specifically, in this case Baz In this way:
package org.example; import static org.example.Type.BAZ; import java.awt.Frame; public class Foo extends Frame { public Foo() { System.out.println(BAZ); } }
If you have a bunch of members, it will be a painful neck Another method may be to make the type interface, and then let foo implement the interface:
public interface Type { public static final int BAZ = 1; } public class Foo extends Frame implements Type{ public Foo() { System.out.println(BAZ); } }
You can also create a type instance in foo class, rename type to avoid conflict, or create a bridge between Foo and type
These are trivial solutions, not extension frameworks