Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a static class illegal in Java?

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.

like image 695
JDx Avatar asked Aug 06 '12 15:08

JDx


People also ask

Why You Should Avoid static classes?

Static classes have several limitations compared to non-static ones: A static class cannot be inherited from another class. A static class cannot be a base class for another static or non-static class. Static classes do not support virtual methods.

What happens if we declare class as static in Java?

In Java, static is a keyword that can be used with variables, classes, blocks, and methods. When we use the static keyword before any of them, it means that specified member belongs to a type itself. In other words, an instance of a static member is created and shared across all the instances of the class.

Can you have a static class in Java?

Java allows a class to be defined within another class. These are called Nested Classes. Classes can be static which most developers are aware of, henceforth some classes can be made static in Java. Java supports Static Instance Variables, Static Methods, Static Block, and Static Classes.


2 Answers

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.

like image 85
Sanjay T. Sharma Avatar answered Oct 03 '22 19:10

Sanjay T. Sharma


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?

Let me show you how a static / non static inner class is used in an example:

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     } } 
like image 33
Balázs Édes Avatar answered Oct 03 '22 19:10

Balázs Édes