次のコードがNullpointerExceptionsを投げつづけているのは絶対的にわかりません。私はこれを理解したりデバッグすることができませんでした。(より大きいクラスの下位コードは削除されました)..."this"リファレンスをJavaコンストラクタでArrayに追加できません
コードは "Enum Pattern"に基づいており、すべての "Constants"クラスに含まれている(私はこれのためにリフレクションを使用しますが一覧/地図を使用している可能性がはるかに簡単です...)
public class Country {
public static final Country SWITZERLAND = new Country("SWITZERLAND");
private static ArrayList<Country> countries = new ArrayList<Country>();
private Country(String constname) {
//constname is currently not used (I will use it for a Key in a Map)
System.out.println(constname);
System.out.println("Ref debug:"+this);
//Ad this to the Countries
Country.countries.add(this);
}
}
ヘルプは非常に高く評価されるだろう。私はここで何が欠けていますか?
これはゲラット学習でした:それを要約すると、静的イニシャライザの順序は関係あります!!! お返事いただきありがとうございます。 – jan
自己登録コンストラクタは実際には良い考えではありません。 –