JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Inner Classes
  • Nested classes which are not static are called inner classes.
  • There are 3 kinds of inner classes:
    • non-static member class, a.k.a. (regular) inner class
    • local class
    • anonymous class
  • An instance of an inner classes is associated with an instance of the enclosing class.
  • Consequentially, inner classes can access non-static members of the enclosing class.
  • Inner class is the java class  inside the other class.
  • It is a member of the outer class in the same way that the instance variables and
  • methods of the outer class are members
  • An inner class is local to the outer class definition.
  • The name of an inner class may be reused for something else outside the outer class definition.
  • If the inner class is private, then the inner class cannot be accessed by name outside the definition of the outer class.
There are two types of nested classes:
  • static 
  • non-static. 

  • A static nested class is one which has the static modifier applied.
  •  Because it is static, it must access the members of its enclosing class through an object.
  •  That is, it cannot refer to members of its enclosing class directly. 
  • Because of this restriction, static nested classes are seldom used

Local Classes

  • Defined inside a method, and only visible from the point it is declared to the end of the method.
  • If defined inside a non-static method, is considered an inner class
    and has access to non-static members otherwise, behaves more like static nested class.
  • Can access parameters as well as local variables declared in the same method
    before the class is declared, but only as long as they are marked final.
  • Defined inside a method, and only visible from the point it is declared to the end of the method.
  • If defined inside a non-static method, is considered an inner class and has access
    to non-static members otherwise, behaves more like static nested class.
  • Can access parameters as well as local variables declared in the same method
    before the class is declared, but only as long as they are marked final.

Anonymous Classes

  • Local classes are useful when you need to use some local variable inside it.
  • However, typically only one instance of these classes are needed per method invocation.
  • Anonymous classes are also created inside the body of a method, however
    • Defining the class also defines an instance of it
    • No more instances can be defined from that class
  • Probably the most complicated of the nested class types, and probably the most useful.
Anonymous Classes -Syntax

new<parent class interface>(<arguments>)
 {
<anonymous class body>
}

Simple Example:

interfaceA
 
{
    void g();
}

classB
{
    void f()
       {
 
       A a= newA() {
                 void g()
                       {}
                     }
;
     a.g
();
}
}

Anonymous Classes & Constructors
  • Anonymous classes cannot have constructors* (they don’t even have a name).
  • Constructors of a parent class can be invoked using the argument list.

Example
/*
 * @author shankar jaiswal
 *www.jaiswaltraining.com
 */
interface myinter{
  void show();
}
public class anony1 {
  
myinter i=new myinter() {
  
  @Override
  public void show() {
    System.out.println("dis method");    
  }
};

public static void main(String[] args) {
  anony1 a=new anony1();
  a.i.show();
  }
}

Output





01 /*
02  * @author shankar jaiswal
03  *www.jaiswaltraining.com
04  */
05 interface myinter{
06   void show();
07 }
08 public class anony1 {
09   
10 myinter i=new myinter() {
11   
12   @Override
13   public void show() {
14     System.out.println("dis method");    
15   }
16 };
17 
18 myinter i1=new myinter() {
19   
20   @Override
21   public void show() {
22     System.out.println("dis method");    
23   }
24 };
25 
26 
27 public static void main(String[] args) {
28   anony1 a=new anony1();
29   a.i.show();
30   }
31 }

Download Code



inner class can access  outer class member

  • inner class is the member of outer class.
  • So it can use outer class member.


/*
 * @author shankar jaiswal
 * www.hjaiswaltraining.com
 */
public class myouter {
  String name = "shankar";

  class myinner {
    void show() {
      System.out.println(name);
    }
  }

  public static void main(String[] args) {
    new myouter().new myinner().show();
  }
}

Output

java myouter
shankar
Download Code

Example:-


public class {

  void show(){System.out.println("outer show");}
  class B{
    void show(){System.out.println("inner show");}
    void aa(){
      System.out.println("in aaa");
      //show();
      //A.this.show();
      B.this.show();
    }    
  }  
  public static void main(String[] args) {
    A.B aa=new A().new B();
    aa.aa();
  }
}
Output:-


java A

in aaa
inner show

Doanload Code

Example:-

/*
 * @author shankar jaiswal
 *www.jaiswaltraining.com
 */
class myinter1{
   void show(){}
}
public class anony2 {
  
myinter1 i=new myinter1() {
  
  @Override
  public void show() {
    System.out.println("disp method");    
  }
};

myinter1 i1=new myinter1() {
  
  @Override
  public void show() {
    System.out.println("disp.... method");    
  }
  
  void aaa(){
    System.out.println("in aaaaaaa.....");
  }
};


public static void main(String[] args) {
  anony2 a=new anony2();
  a.i.show();
  }
}

Output:-

java anony2

disp method

Download Code

Example:-

/*
 * @author shankar jaiswal
 *
 */
public class localouter1 {
  int x = 88;

  void show() {
    final int age=33;
    class inner1 {
      String name = "mohan";

      void disp() {
        System.out.println(x++);
        System.out.println(name);
        System.out.println(age);
      }
    }
     inner1 i=new inner1();
     i.disp();
  }

  public static void main(String[] args) {
  localouter1 lo=new localouter1();
  lo.show();
    
  }
}

Output:-


java localouter1

88
mohan
33

Downlaod Code

Example:-

public class mystatic {
static int x=888;
  static
  class stinner{
    
  static
  void show(){
      
      System.out.println(x);
    }
  }
  
  public static void main(String[] args) {
    stinner.show();
    //mystatic.stinner a=new mystatic().new stinner();
    //a.show();
    }
  void disp(){
  //mystatic.stinner a=new mystatic().new stinner();
  //a.show();
  }
}
Output:-

java mystatic
888
Download Code