While and do-while

This page discusses - While and do-while

While and do-while

While and do-while

     

Nested classes

Here is another advantage of the Java, object-oriented programming language that allows us to define a class within another class, such class is called a nested class. Inner classes can be either named or anonymous. We'll discuss on the topic of anonymous classes later in this section. A named inner class looks just like any other class, except that it is nested inside another class. ( It can even contain further levels of nested classes, but you shouldn't carry these things too far ).

A simple nested class example is given below:

class OuterClass {
  ...expression...
  class NestedClass {
  ...expression...
  }
}

There are two categories of nested classes
i)  Static classes
ii) Inner classes ( Non-static )

class OuterClass { ... static class StaticNestedClass { ... } class InnerClass { ... } }

i) Static Nested Classes

In simple word a class that is declared static is called static nested class. Static nested classes can be allocated independently of any particular outer class object. A static nested class use the instance variables or methods defined in its enclosing class only through an object reference. A static nested class interacts with the instance members of its outer class or any other class just like a top-level class.

Static nested classes can be access using the enclosing class name:

OuterClass.StaticNestedClass

If we want to make a object of the static nested class then we have to write down the following line:

OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

The advantage of a static nested class is that it doesn't need an object of the containing class to work. This can help you reduce the number of objects your application creates at runtime.

ii) Inner Nested Classes

An inner class is a nonstatic class declared inside another class. It has access to all of its enclosing class's instance data, including private fields and methods. Inner classes may access static data but may not declare static members unless those members are compile time constants As with instance methods and variables, an inner class is associated with an instance of its enclosing class and has direct access to that object's methods and fields. Also, because an inner class is associated with an instance, it cannot define any static members itself.

Objects that are instances of an inner class exist within an instance of the outer class. Consider the following classes:

class OuterClass {
  ...
  class InnerClass {
  ...
  }
}

The most common use of inner classes is as event handlers for GUI-based applications.

These classes are usually declared anonymously and as needed for each component that requires an event handler.

The advantage of using an inner class for event handling is that you can avoid large If/else statements to decide which component is being handled.  Each component gets its own event handler, so each event handler knows implicitly the component for which it's working.

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. The next figure illustrates this idea.

An InnerClass Exists Within an Instance of OuterClass.

An InnerClass Exists Within an Instance of OuterClass

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

OuterClass.InnerClass innerObject = outerObject.new InnerClass();
Additionally, there are two special kinds of inner classes: local classes and anonymous classes (also called anonymous inner classes). Both of these will be discussed briefly in the next section.

 

Why Use Nested Classes?

There 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.
  • To reduce the creation of .java files.
  • Nested classes can lead to more readable and maintainable code.

More readable, maintainable code-Nesting small classes within top-level classes places the code closer to where it is used.

 

The static nested class can access the various static members of the enclosing class. However, unlike inner classes, it cannot access any instance variables in the enclosing class. It is an important difference between static nested classes and inner classes.