Reputation: 8972
Why does this:
package com.example;
import com.example.Foo.Bar.Baz;
import java.io.Serializable; // I did import Serializable...
public class Foo implements Serializable {
public final Bar bar;
public Foo(Bar bar) {
this.bar = bar == null ? new Bar(Baz.ONE) : bar;
}
public static class Bar implements Serializable { // this is line 15, where the compiler error is pointing
public enum Baz {
ONE
}
public final Baz baz;
public Bar(Baz baz) {
this.baz = baz;
}
}
}
Give me this:
[ERROR] <path to file>/Foo.java:[15,44] cannot find symbol
[ERROR] symbol: class Serializable
[ERROR] location: class com.example.Foo
If I replace the Serializable interface to something else like :
public interface MyMarkerInterface {}
then the code compiles. (even Cloneable
works!)
What makes this happen? intelliJ didn't spot anything wrong through static analysis.
Upvotes: 3
Views: 10618
Reputation: 776
in java7 and java8 compiling depended on order of imports. your code works in java >= 9. see https://bugs.openjdk.java.net/browse/JDK-8066856 and https://bugs.openjdk.java.net/browse/JDK-7101822
to make it compile in java7 and java8 just reorder the imports
Upvotes: 0
Reputation: 201497
Don't try and import the internal class. That's causing your compiler error
// import com.example.Foo.Bar.Baz;
import java.io.Serializable;
public class Foo implements Serializable {
public final Bar bar;
public Foo(Bar bar) {
this.bar = bar == null ? new Bar(Bar.Baz.ONE) : bar;
}
public static class Bar implements Serializable {
public enum Baz {
ONE
}
public final Baz baz;
public Bar(Baz baz) {
this.baz = baz;
}
}
}
Upvotes: 9