Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Nested classes(inner class) in java

Sat Jan 31, 2009 2:29 am

Nested (inner ) classes:
You can create a class inside other class .This class is called nested class and the main class is called outer class

For example:

Code:
class <OuterNameofClass> {
// variables and methods for the outer class

class <NestedNameofClass> {
// variables and methods for the nested class

}
}
   


Nested class is a member of the outer class and can see all other members of the class (methods and variables). Nested class can be static or non static .You may ask when I need a nested class? You may need it in cases that the nested class depends in the outer class in its functionality and existence.

Rules :
  • if you have a static nested class, you can't access the methods and variables of the outer class from the static nested (inner) class.
  • You can't create a static member inside the nested class.
  • To have instance of nested class you must have a instance of outer class.
  • Nested class can be declared abstract or final.

Code:

public class Main 
{
 
     
            
    public static void main
(String[] args) {

            outterclass outobj=new outterclass();
            outterclass.innerclass innerobj=outobj.new  innerclass();
    }
}

class outterclass {

    class innerclass
    
{
        
    
}
}
 




Post a reply
  Related Posts  to : Nested classes(inner class) in java
 java wrapper classes     -  
 Wrapper classes use in java     -  
 instantiating java classes     -  
 java abstract class,concrete class and interface     -  
 java.util classes and interfaces support event handling     -  
 Nested function     -  
 Break in Nested Loops     -  
 nested ordered list     -  
 Layout with Nested JPanels     -  
 Java Television class     -  

Topic Tags

Java OOP