Вопрос по static, android, java – Недопустимая ошибка модификатора для статического класса
Я разрабатываю приложение для Android, но наткнулся на кирпичную стену, но получаю сообщение об ошибке:
Illegal modifier for the class FavsPopupFragment; only public, abstract & final are permitted
Это произошло послеэтот ответ на другой ТАК вопрос. Вот код, который у меня есть:
package com.package.name;
/* Imports were here */
public static class FavsPopupFragment extends SherlockDialogFragment {
static FavsPopupFragment newInstance() {
FavsPopupFragment frag = new FavsPopupFragment();
return frag;
}
}
Ошибка появляется в имени класса. Я не понимаю, почему это не работает, пожалуйста, помогите. Спасибо.
FavsPopupFragment
внутри другого класса.
Sanjay T. Sharma
это то, что компилятор пытается вам сказать. Также посмотрите на ответВот относительноwhy В этом случае. Суть это:
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.
static
модификатор для классов верхнего уровня, хотя могут быть вложенные классы, которые могут быть изменены с помощьюstatic
ключевое слово.
В этом случае вам либо нужно удалить статический модификатор, либо убедиться, что этот класс вложен в другой класс верхнего уровня.
Extra info
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
вы не можете использовать ключевое слово 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
}
}
классы может быть статичным.
for the class FavsPopupFragment; only public, abstract & final are permitted