News:

Check out the recently added slayer skill, and make sure to vote in the Future Updates section for new content!

Main Menu

Java Tutorials: Series tutorial Four: Nested Classes.

Started by Tonypker, February 12, 2009, 11:53:48 PM

Previous topic - Next topic

Tonypker

So, I just found out what a nested class is. It seems a bit useful, so I'll show you some parts of it.

A Nested class, is one class defined inside of another class. This is used in the Java, and C programming language.

Nested classes can be 2 things, static or non static.

Nested classes declared static, are called static nested classes.
Nested classes delcared non-static, are called inner classes.


Example:

class outClass {
    //whatever can go here
    class nestedClass {
        //whatever can go here
    }
}


A nested class is a member of its enclosing class.
that might be hard to understand, but here's my explanation.

See that above code? The nested Class is a member of the out Class, because the nested one is enclosed inside it. It's that easy :)

From Java
QuoteNon-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private.

QuoteThere are several compelling reasons for using nested classes, among them:

    * It is a way of logically grouping classes that are only used in one place.
    * It increases encapsulation.
    * Nested classes can lead to more readable and maintainable code.

Hope it helped, -Tony ;)