inner_named_class.htm | |||||||||||||||
A class defined within another class is called a nested class. Like other
members of a class, a nested class can be declared static or not.
A non-static nested class is called an inner class. An instance of an inner class can exist only within an instance of its
enclosing class and has access to its enclosing class's members even if
they are declared private.
The following table shows the types of nested classes:
|
|||||||||||||||
Inner Named class | |||||||||||||||
class Outer { Outer() { Inner i = new Inner(); i.innerMethod(); } // Inner class private class Inner { Inner() {} public void innerMethod() { // Do something. } } } |
|||||||||||||||
View of Codes Snippets:
Calling inner class
|
|||||||||||||||
Complete Codes :
import java.io.*; import javax.swing.JOptionPane; import java.net.URI; public class ClassTemplate1 { public ClassTemplate1() { // TODO Auto-generated constructor stub } public static void main(String[] args) throws Exception { // TODO Auto-generated method stub Process pp = new Process(); // can't instantiate abstract class try { pp.read_line(); // } catch (NumberFormatException exc) { // } } }// end of class template // //outer class class Process { public Process() { System.out.println("Process outer constructor"); } public void read_line() { System.out.println("Reading Process outer class :"); Inner in = new Inner(); in.rea_inner(); } private class Inner //class inner named { public Inner() { System.out.println("Process inner constructor");} private void rea_inner() { System.out.println("Process inner reader"); } } } |
|||||||||||||||
![]() |
|||||||||||||||