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
Is arraylist ordered?
Where are register variables stored?
Why arraylist is not synchronized in java example?
Define "Access specifiers" in java.
How to convert string to byte array and vice versa?
What are three ways in which a thread can enter the waiting state in java programming?
How does queue work in java?
How do you start a new line in java?
How is the marker interface used in Java?
What is memory leak and how does java handle it?
What are dot operator queries?
What is the meaning of immutable regarding string?
Explain about abstract classes in java?
Explain wait(), notify() and notifyall() methods of object class ?
When should the method invokelater() be used?