Inner classes in Java

When a non-static class is nested inside a class, it is called as inner class.

Inner class can only be accessed using any object of outer class.

General Syntax

class Outer_class{

class Inner_class {

//statements

}

}

To access the members of inner class,

Example

class State{

class City{

void inside(){

System.out.println("Inside city");

}

}

public static void main(String args[]){

State st=new State();

State2.City ct=st.new City();

ct.inside();

}

}

Output

Inside city


We have special kinds of inner classes - local inner class and anonymous inner class which will be discussed in next topics.