2012-08-06 19 views
20

私はアンドロイドのアプリケーションを開発していますが、レンガの壁のビットを直撃している、私はエラーを取得しておいてください。不正な修飾エラー

Illegal modifier for the class FavsPopupFragment; only public, abstract & final are permitted 

これは、別のSOの質問にthis answerに従った後に起こりました。私が持っているコードは次のとおりです。

package com.package.name; 

/* Imports were here */ 

public static class FavsPopupFragment extends SherlockDialogFragment { 

    static FavsPopupFragment newInstance() { 
     FavsPopupFragment frag = new FavsPopupFragment(); 
     return frag; 
    } 
} 

エラーがクラス名に表示されます。私はなぜこれは動作しません理解していない、助けてください。ありがとうございました。

+1

その場合、 'FavsPopupFragment'を別のクラスの中に置くようにしなければなりません。 –

+0

私には、この質問を見ているのは、私がC#/ .Netから来ているということです。そこに静的トップレベルクラスを置くことができます。 「静的」とは、2つの技術の2つの異なることを意味しますが、 – RenniePet

答えて

38

トップレベルの静的クラスを作成することはできません。これはコンパイラがあなたに伝えようとしていることです。 なぜであるかについては、回答hereをご覧ください。要点は:

What the static boils down to is that an instance of the class can stand on its own. Or, the other way around: a non-static inner class (= instance inner class) cannot exist without an instance of the outer class. Since a top-level class does not have an outer class, it can't be anything but static.

Because all top-level classes are static, having the static keyword in a top-level class definition is pointless.

1

クラス定義から静的を削除します。ネストされたclassesのみが静的になります。

for the class FavsPopupFragment; only public, abstract & final are permitted

1

新しいキーワードを使用して静的クラスのインスタンスを作成することはできません。これはとにかくフラグメントなので、とにかく静的であってはいけないはずです。

1

staticキーワードで変更できるネストされたクラスがありますが、最上位クラスにはstatic修飾子を使用できません。

この場合、静的修飾子を削除するか、このクラスが別のトップレベルクラスにネストされていることを確認する必要があります。

追加情報

There's no such thing as a static class. The static modifier in this case (static nested) says that the nested class is a static member of the outer class. That means it can be accessed, as with other static members, without having an instance of the outer class.

Just as a static method does not have access to the instance variables and nonstatic methods of the class, a static nested class does not have access to the instance variables and nonstatic methods of the outer class

1

1.staticPackage level.

2.staticで使用することはできませんクラスのレベル内で可能です。

3.しかし、クラスがinner class、すなわちあるときあなたはまだ、クラスに静的を使用することができます。 (static inner class)、一般にとして知られています。

3

前述の回答と同様に、トップレベルクラスではstaticキーワードを使用できません。しかし、なぜ私はそれが静的であることを望んだのだろうか?

私は、静的/非静的内部クラスは、例でどのように使用されるかをお見せしましょう:

public class A 
{ 
    public class B{} 

    public static class C{} 

    public static void foo() 
    { 
     B b = new B(); //incorrect 

     A a = new A(); 
     A.B b = a.new B(); //correct 

     C c = new C(); //correct 
    } 
    public void bar() 
    { 
     B b = new B(); 
     C c = new C(); // both are correct 
    } 
} 

、完全に別のクラスから:

public class D 
{ 
    public void foo() 
    { 
     A.B b = new A.B() //incorrect 

     A a = new A() 
     A.B b = a.new B() //correct 

     A.C c = new A.C() //correct 
    } 
} 
1
  • staticを使用することができます内部階級レベルで。トップレベルはstaticにすることはできません。前述のように、public,abstract & finalのみが許可されます。

  • staticは、メソッドと変数のクラスレベルで主に使用されます。

1

トップレベルのクラスはすでに定義済みであるため、静的宣言には意味がありません。そうすることは誤りです。コンパイラはこのエラーを検出して報告します。