Home Java Beginners Inner class in java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Inner class in java

Advertisement
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 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

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 24, 2013

Ask Questions?    Discuss: Inner class in java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com