Monday, June 16, 2008

Nested Classes in Java? Inner Classes in Java?


Nested Classes in Java

A nested class is a class defined inside the definition (body) of another enclosing class. A nested class can be of two types - static or non-static. A nested class is treated as a member of the enclosing class. A static nested class is not very tightly integrated with the enclosing class and it lives its own independent life as well i.e., a static nested class can be instantiated like any other class from just about anywhere. They are defined inside the definition of an enclosing class just to get a logical grouping of the classes, which in turn increases readability and provides better packaging convenience.

A static nested class can't directly access the members of the enclosing class. Like any other top-level class it needs to access the members of the enclosing class via object references only.

Example: a static nested class

class EnclosingClass{
...
...
static class StaticNestedClass{
//...definition of the static nested class
...
...
}
...
...
}

A static nested class is accessed using the enclosing class name as follows:

EnclosingClass.StaticNestedClass staticNestedObjectRef = new EnclosingClass.StaticNestedClass();

Inner Classes in Java

A non-static nested class is called an inner class and it's tightly integrated with the enclosing class unlike a static nested class. An inner class instance can't exist independent to the enclosing class instance. An inner class instance always exist within the instance of the enclosing class. Since, it's always associated with the enclosing class instance and it has direct access to all the members of the enclosing class - even the private members.

Example: an inner class

class EnclosingClass
{
...
...
class InnerClass{
//...definition of the inner class
...
...
}
...
...
}

Since, an instance of an inner class always exists within an instance of the enclosing class, hence we must instantiate the enclosing class first and then on that instance we can instantiate the inner class:-

EnclosingClass enclosingObjectRef = new EnclosingClass();
EnclosingClass.InnerClass innerObjectRef = enclosingObjectRef.new InnerClass();

Can an Inner Class declare static members?

No. Because any instance of an inner class is always associated with an instance of the enclosing class.

Can we have private or protected access for classes in Java?

Yeah... but only for nested classes and not for top-level classes. Nested classes are treated as members of the enclosing classes and hence we can specify any of the four access specifiers - private, package, protected, or public. We don't have this luxury with top-level classes - they can only be declared public or package.



Share/Save/Bookmark


5 comments:

Prashant said...

good work geek, you have made a fairly complex topic easy to understand.

Geek said...

Thanks Prashant.

Anonymous said...

Thanks Geek...

Emi said...

Thanks for this very nice explanation, it enlightened me about a java topic that i knew almost nothing about =)

Emi said...

Thanks for this very nice explanation, it enlightened me about a java topic that i knew almost nothing about =)