Total members 11895 |It is currently Sat Dec 21, 2024 2:26 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





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
    
{
        
    
}
}
 




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  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
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com