ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

Inner Classes

Java Inner Class
Java inner class can be called as nested class i.e. declared inside the class or interface.
We use inner classes to group classes and interfaces in one place so that it can be more readable,understandable and maintainable.
It also allows access to all the members of outer class including even private data members and methods.
Syntax of Inner class

  1. class Java_Outer_class{
  2. //code
  3. class Java_Inner_class{
  4. //code
  5. }
  6. }

Advantage of java inner classes
1) Nested classes displays a special type of relationship that is; it can access all the members in a given class (data members and methods) of outer class including even private.
2) Nested classes helps in more readable, understandable and maintainable code as it group classes and interfaces in one place only on the basis of logic provided by the user.
3) Code Optimization: less code to write giving minimum inout and helping to get maximum output.
Difference between nested class and inner class in Java
Inner class is a part of nested class. Non-static nested classes are known as inner classes.

Types of Nested classes
There are two types of nested classes non-static and static nested classes.The non-static nested classes are also known as inner classes.
1. Non-static nested class(inner class)
o a)Member inner class
o b)Annomynous inner class
o c)Local inner class
2. Static nested class

  1. class Outer_Fan{
  2. int a;
  3. private class Inner_Fan{{
  4. public void print(){
  5. System.out.println("Inner Class");
  6. }
  7. }
  8. void display_Inner(){
  9. Inner_Fan inner = new Inner_Fan();
  10. inner.print();
  11. }
  12. }
  13. public class Fan1{
  14. public static void main(String args[])
  15. {
  16. Outer_Fan outer=new Outer_Fan();
  17. outer.display_Inner();
  18. }
  19.  
  20. }

Output:“Inner Class “

Facebook Comments

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!