Nested classes: Examples and tutorials

Nested classes

Nested classes: Examples and tutorials

Nested classes

Nested classes: Examples and tutorials

Nested classes: Examples and tutorials

     

  1. Nested classes
    Here is another advantage of the Java, an 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.
          
  2. Static Nested Classes
    A nested class that is declared static is called a static nested class. Memory to the objects of  any static nested classes are 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.
      
  3. Inner Nested Classes
    Non-static nested classes are slightly different from static nested classes, a non-static nested class is actually associated to an object rather than to the class in which it is nested.
       
  4. Member Classes
    The class which is define in the body of a class is called a member class. Member class can be used within the body of the same class. The member classes can also use of instances of the same class without explicit delegation. The member class is the only class that can be declare as static.
        
  5. Local classes 
    Local classes are same as local initializer and is declared within a code body (inside a code body of a function or static initializer) of  Java. Very rarely local class is defined within a method. The lacal class can be instantiated so many times as we wish to initialize.
        
  6. Anonymous Classes
    Anonymous classes can be extend a super class and can also implement an interface. We can say that it is a local class that is defined without a class name, the anonymous classes  are instantiated only once