Working With Nested Class in Java

Introduction

 
In this article, we are going to describe the concept of a Nested class. The Java programming language allows you to define a class within another class. Such a class is called a nested class. Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are simply called static nested classes. Non-static nested classes are called inner classes.
 
  DSKZJLfjghihrsdr.gif  

Reasons for using 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.
  • Nested classes can lead to more readable and maintainable code.
A normal class is a direct member of a package, a top-level class. Inner classes, which became available with Java 1.1, come in four flavors:
  • Static member classes
  • Member classes
  • Local classes
  • Anonymous classes

Static Member classes

 
In a nested scenario, a static member class is treated as a static member of a class in which that class is nested like other static methods and static variables. A static member class has access to all static methods of the parent, or top-level, class.
 
Syntax
 
public class Temp
{
//  class functionality 
private static class StaticNested
   {
// you cna put his functionality
   }
}
 

Member classes

 
Member class is also defined as a member of a class. Unlike the static variety, the member class is specific to an instance and has access to any and all methods and members, even the parent's this reference.
 
Example 
  1. public class Top {    
  2.  int x = 10    
  3.  class Member {    
  4.   public metho1() {    
  5.    system.out.println("this is member class");    
  6.   }    
  7.  }    
  8.  public static void main(String arg[]) {    
  9.   Member m = new Member();    
  10.   m.method();    
  11.   System.out.println("this from top class");    
  12.  }    
  13. }     

Local class

image04.gif
 
In a local class, the class can be declared inside any function or any block. And this is not visible from anywhere only, instances of this class cannot be created and used within the scope in which it is declared. This can be useful if you need to hide an ancillary object, which should not be accessible or used anywhere.
  1. public java.util.Enumeration enumerate() {    
  2.  class Enumerator implements java.util.Enumeration {    
  3.   Linkable current;    
  4.   public Enumerator() {    
  5.    current = head;    
  6.   }    
  7.   public boolean hasMoreElements() {    
  8.    return (current != null);    
  9.   }    
  10.   public Object nextElement() {    
  11.    if (current == nullthrow new java.util.NoSuchElementException();    
  12.    Object value = current;    
  13.    current = current.getNext();    
  14.    return value;    
  15.   }    
  16.  }    
  17.  return new Enumerator();    
  18. }     

Anonymous class

 
An Anonymous class is nothing. It is a local class but without a name. In other words, an anonymous class is a local class without a name. An anonymous class is defined and instantiated in a single succinct expression using the new operator.
 
Syntax
 
submittButton.addKeyListener ( new KeyListener) { 
public void actionPerformed ( KeyEvent event )
{
   Object o = event.getSource();
   if
( o == submittButton )
   { 
   System.out.println("i am sucess");
   }
}
}


Similar Articles