35

I'm developing an Android application but have hit a bit of a brick wall, I keep getting the error:

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

This happened after following this answer to another SO question. Here is the code that I have:

package com.package.name;

/* Imports were here */

public static class FavsPopupFragment extends SherlockDialogFragment {

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

The error appears on the class name. I don't understand why this won't work, please help. Thank you.

Pawel Veselov
  • 3,996
  • 7
  • 44
  • 62
JDx
  • 2,615
  • 3
  • 22
  • 33
  • 1
    In that case, you have to ensure or force yourself to put `FavsPopupFragment` inside another class. – Sanjay T. Sharma Aug 06 '12 at 16:04
  • For me the reason that I'm looking at this question is that I'm coming from C#/.Net, and there you can have static top level classes. Although it turns out that "static" means two different things in the two technologies. – RenniePet Sep 24 '14 at 21:54

9 Answers9

65

You can't create a top level static class; that's what the compiler is trying to tell you. Also have a look at the answer here as to why this is the case. The gist is:

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.

Community
  • 1
  • 1
Sanjay T. Sharma
  • 22,857
  • 4
  • 59
  • 71
9

As the previous answers stated, you can't use the static keyword in top level classes. But i wonder, why did you want it to be static?

An example of how static/non static classes can be instantiated:

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
    }
}

And from a completely different class:

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
    }
}
Balázs Édes
  • 13,452
  • 6
  • 54
  • 89
2
  • static can be used at the inner class level. Top level can't be static, as said before, only public, abstract & final are permitted.

  • static is mainly used inside class level for methods and variables.

jrudolph
  • 8,307
  • 4
  • 32
  • 50
Jyoti Prakash
  • 3,921
  • 3
  • 21
  • 24
2

A top-level class is by definition already top-level, so there is no point in declaring it static; it is an error to do so. The compiler will detect and report this error.

Andrew Glukhoff
  • 898
  • 11
  • 9
1

Remove static from class definition. Only nested classes can be static.

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

kosa
  • 65,990
  • 13
  • 130
  • 167
1

I don't think you can create instances of a static class using the new keyword. This is a fragment anyway, so it probably should not be static anyway.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
1

You cant use static modifier for top level classes, though there can be nested classes which can be modified with static keyword.

In this case either you need to remove the static modifier, or make sure this class nested into another top level class.

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

code-jaff
  • 9,230
  • 4
  • 35
  • 56
1

1. static canNOT be used at Package level.

2. static is possible within the Class level.

3. But you can still use static on a class, when the class is an inner class, ie. (static inner class), commonly known as Top level class.

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

The access modifier supported for top level are class are as follows :

1) public

2) default

3) abstract

4) final

5) strictfp.

Reason: Top level class

Because the static keyword is meant for providing memory and executing logic without creating Objects, a class does not have a value logic directly, so the static keyword is not allowed for outer class and mainly as mentioned above static can't be used at Package level. It only used within the Class level.

Vishal Sheth
  • 163
  • 1
  • 7