Inner class in java

In this example we will describe inner classes in java. Inner class declared inside a class. The inner class also call Nested classes. Inner classes provides an graceful and powerful feature to the Java. Inner class parts of Nested Class.

Inner class in java

In this example we will describe inner classes in java. Inner class declared inside a class. The inner class also call Nested classes. Inner classes provides an graceful and powerful feature to the Java. Inner class parts of Nested Class.

Inner class in java

Inner class in java

In this example we will describe inner classes in java. Inner class declared inside a class. The inner class also call Nested classes. Inner classes provides an graceful and powerful feature to the Java. Inner class parts of Nested Class.

Inner classes are related by different names in different situations:

  • Static member classes
  • Member classes
  • Local classes
  • Anonymous classes

Static member classes:-

This class (or interface) is defined as a static member variable of another class.

Member classes:-

This is sometimes defined as a non-static member of an enclosing class. This type of inner class is analogous to an instance method or field.

Local classes:-

This class is defined within a block of Java code so like a local variable, it is visible only within that block.

Anonymous classes:-

An anonymous class is a local class having no name; Syntactically it combines the syntax required for defining a class and the syntax required to instantiate an object.

Inner classes defined:-

     We can Declare following access modifiers: public, protected, private, or with default package access.

Syntax of inner classes:-

class OuterClassName{
.....
class innerClassName{
.....
}
.....

}

Example of Simple Inner Classes

package InnerClass;

public class InnerClassExample{
	
		public void max() {
	        System.out.println("Outer class");
		}
		
		public class ReallyInner {
		    public void max() {
		        System.out.println("Inner class");
		    }
}
		  public InnerClassExample() {	
		}
		 public  static void main(String [] args){
			 InnerClassExample a = new InnerClassExample();
			//ReallyInner b = new ReallyInner();
		         a.max();
		     
		 }
}

Output of Inner Class

Download Source Code