This question already has an answer here:
-
Why can't a Java class be declared as static?
5 answers
Why can't we create static classes which are outer. I read answers to the question in this community but I haven't gotten a perfect answer. Can some help me with an exact answer
static class A
{
void display()
{
System.out.println("This is a static class");
}
}
Outer classes are already effectively static
A static
nested class means it has no reference to the outer class. An outer class already has no implicit reference to another class.
What exactly would a static class do? Would it be the "Exact same implementation across all instances"? Think about it :)
Because this is pointless and useless. If you want to have static object, just make static instance of class. And if you want to invoke class methods without reference to it's object, just make this method static. And then you will have:
class A
{
public static void display()
{
System.out.println("This is a static method");
}
}
You can invoke this method by A.display();
.
A static nested class is simply namespaces at work. The nested class is name-scoped within the enclosing class and you can think of the outer class as being already present at root namespace of that package.
Hence, no static
keyword is required since the class is already global within the package and through-out the application if public.
There are no outer static classes in Java.
Because all outer classes are already visible like the static modifier would do.
But of you mean, that you don't want to need an instance of the class you can simply make all Methods and variables in the class static.