What is a static inner class? Illustrate with an example. What is the utility of the collections API?
A static inner class behaves like any "outer" class. It may contain methods and fields.
It is not necessarily the case that an instance of the outer class exists even when we have created an instance of the inner class. Similarly, instantiating the outer class does not create any instances of the inner class.
A Static Inner Class is a nested class. Because it is static it must access the members of its enclosing class through an Object. It cannot refer to members of its enclosing class directly.
Moreover A non-Static nested class is called inner class.
It has access to all of the variables and methods of its enclosing or outer class and may refer to them directly in the same way that other non static members of the outer class do.
Consider the following Java code fragment:
public class A
{
int y;
public static class B
{
int x;.
void f () {}
}
}
No comments :
Post a Comment