Answer Posted / sathishkumarbabu
All top-level classes are, by definition, static.
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.
Some code to play around with:
public class Foo {
public class Bar {
// Non-static innner class
}
public static class Baz {
// Static inner class
}
}
public class Example {
public static void main(String[] args) {
new Foo(); // this is ok
new Foo.Baz(); // this is ok
new Foo.Bar(); // does not compile!
Foo f = new Foo();
Foo.Bar bar = f.new Bar(); //this works, but don't do this
}
}
I put the "but don't do this" in there because it's really ugly code design. Instance inner classes should not be visible outside the outer class. They should only be used from within the outer class.
Regards : Barend Garvelink
| Is This Answer Correct ? | 3 Yes | 4 No |
Post New Answer View All Answers
How does multithreading take place on a computer with a single cpu?
What are filterstreams?
What is a finally block? Is there a case when finally will not execute?
What are kinds of processors?
What do you mean by stream pipelining in java 8?
What is difference between identifier and variable?
Can we catch more than one exception in a single catch block?
What is the difference between final, finally and finalize()?
Can we sort set in java?
What is json parser in java?
Can you explain the usages of class.forname()?
What are thread priorities and importance of thread priorities in java?
If an application has multiple classes in it, is it okay to have a main method in more than one class?
Why java is called not pure object oriented language?
Is null an object in java?