Почему это:
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;
}
}
}
Дайте мне это:
[ERROR] <path to file>/Foo.java:[15,44] cannot find symbol
[ERROR] symbol: class Serializable
[ERROR] location: class com.example.Foo
Если я заменил интерфейс Serializable на другое:
public interface MyMarkerInterface {}
то код компилируется. (даже Cloneable
работает!)
Что это делает? intelliJ не обнаружил ничего плохого в статическом анализе.
Не пытайтесь импортировать внутренний класс. Это вызывает ошибку компилятора
// 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;
}
}
}
Cloneable
или любой другой интерфейс работаетSerializable
(и использоваться) до того, как вы импортируетеSerializable
? затмение переупорядочило их.