JDx*_*JDx 26 java static android
我正在开发一个Android应用程序,但已经打了一个砖墙,我不断收到错误:
Illegal modifier for the class FavsPopupFragment; only public, abstract & final are permitted
Run Code Online (Sandbox Code Playgroud)
这是在对另一个SO问题的回答之后发生的.这是我的代码:
package com.package.name;
/* Imports were here */
public static class FavsPopupFragment extends SherlockDialogFragment {
static FavsPopupFragment newInstance() {
FavsPopupFragment frag = new FavsPopupFragment();
return frag;
}
}
Run Code Online (Sandbox Code Playgroud)
该错误出现在类名称上.我不明白为什么这不起作用,请帮忙.谢谢.
如前面的答案所述,您不能在顶级类中使用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
}
}
Run Code Online (Sandbox Code Playgroud)
从一个完全不同的类:
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
}
}
Run Code Online (Sandbox Code Playgroud)