• Home
  • /
  • Blog
  • /
  • How Java Developers Define And Use Java Inner Class Or Nested Class In Web Development?
Java Web Development

How Java Developers Define And Use Java Inner Class Or Nested Class In Web Development?

Spread the love

Java Web DevelopmentThis post explains the concept of Java Inner Class or Nested Class in which entire java development community will learn the definition, disadvantages, advantages, and uses of Nested Class. It is important for java developers to understand the subject of Nested Class fully to make use of Java Inner class in java web development.

Advantages of Java inner classes

  • Nested classes represent a special type of relationship that is it can access all the members (data members and methods) of outer class including private.
  • Nested classes are used to develop more readable and maintainable code because it logically group classes and interfaces in one place only.
  • Code Optimization: It requires less code to write.
  • The embedding of inner class into the outer class in the case when the inner class is to be used only by one class i.e. the outer class makes the package more streamlined. Nesting the inner class code where it is used (inside the outer class) makes the code more readable and maintainable.
  • The inner class shares a special relationship with the outer class i.e. the inner class has access to all members of the outer class and still have its own type is the main advantages of Inner class.
  • Inner class is that they can be hidden from the other classes in the same package and still have the access to all the members (private also) of the enclosing class. So the outer class members which are going to be used by the inner class can be made private and the inner class members can be hidden from the classes in the same package. This increases the level of encapsulation.
  • A Static nested class doesn’t depend on the outer class instance, it can run on it’s own. Even a main function can be written inside the static nested class and invoked directly.

Disadvantages of Java Inner Classes

  • Using inner class increases the total number of classes being used by the application. For all the classes created by JVM and loaded in the memory, JVM has to perform some tasks like creating the object of type class. Jvm may have to perform some routine tasks for these extra classes created which may result slower performance if the application is using more number of inner classes.
  • Inner classes get limited support of ide/tools as compared to the top level classes, so working with the inner classes is sometimes annoying for the developer.

Uses of Java Inner classes

  • Inner classes enable you to logically group classes that are only used in one place, increase the use of encapsulation, and create more readable and maintainable code.
  • Use a non-static nested class (or inner class) if you require access to an enclosing instance’s non-public fields and methods. Use a static nested class if you don’t require this access.
  • Anonymous classes are useful for writing small encapsulated “callbacks,” such as enumerations, iterators, visitors, etc. They are also helpful for adding behavior to objects which already have names, such as AWT components, and threads. In both cases, an intervening class name can detract from the clarity of the code.

Java Inner Class

  • Java inner class or nested class is a class i.e. declared inside the class or interface.
  • We use inner classes to logically group classes and interfaces in one place so that it can be more readable and maintainable.
  • Additionally, it can access all the members of outer class including private data members and methods.

Syntax of Inner class

          class Java_Outer_class{

   //code

   class Java_Inner_class{

     //code

   }

}

Types of Nested classes (Inner 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)

        a)Member inner class

        b)Annomynous inner class

        c)Local inner class

2)  Static nested class

1) Member inner class

  • A non-static class that is created inside a class but outside a method is called member inner class.
  • Given below program to create an inner class and access it. In the given example

class OuterClass{

   int num;

   //inner class

   private class InnerClass{{

      public void print(){

      System.out.println(“This is an inner class”);

      }

   }

   //Accessing he inner class from the method within

   void display_Inner(){

      InnerClass inner = new InnerClass();

      inner.print();

   }

}

public class My_class{

   public static void main(String args[]){

      //Instantiating the outer class

      OuterClass outer=new OuterClass();

      //Accessing the display_Inner() method.

      outer.display_Inner();

   }

}

Output :

This is an inner class

  • Here you can observe that OuterClass is the outer class, InnerClass is the inner class, display_Inner() is the method inside which we are instantiating the inner class, and this method is invoked from the mainmethod.

2) Anonymous inner class

  • An inner class declared without a class name is known as an anonymous inner class. In case of anonymous inner classes, we declare and instantiate them at the same time. Generally they are used whenever you need to override the method of a class or an interface.

abstract class Person{

  abstract void eat();

}

class TestAnonymousInner{

   public static void main(String args[]){

     Person p=new Person(){

     void eat(){System.out.println(“Hello…How are you ? “);}

  };

  p.eat();

   }

}

Output :

Hello…How are you ?

3) Local Inner class

  • We can write a class within a method and this will be a local type. Like local variables, the scope of the inner class is restricted within the method.
  • A method-local inner class can be instantiated only within the method where the inner class is defined. The following program shows how to use a method-local inner class.

public class localInner1{

   private int data=30;//instance variable

   void display(){

             class Local{

             void msg(){System.out.println(data);}

  }

  Local l=new Local();

  l.msg();

 }

   public static void main(String args[]){

             localInner1 obj=new localInner1();

             obj.display();

   }

}

Output :

30

4) Static Nested class

  • A static inner class is a nested class which is a static member of the outer class. It can be accessed without instantiating the outer class, using other static members. Just like static members, a static nested class does not have access to the instance variables and methods of the outer class.
  • It cannot access non-static data members and methods. It can be accessed by outer class name.
  • It can access static data members of outer class including private.
  • Static nested class cannot access non-static (instance) data member or method.

class TestOuter1{

  static int data=50;

  static class Inner{

   void msg(){System.out.println(“data is “+data);}

  }

  public static void main(String args[]){

  TestOuter1.Inner obj=new TestOuter1.Inner();

  obj.msg();

  }

}

Output :

data is 30

Entire coding used by senior java developers in this post is for reference purpose only. You can ask queries about Java Inner Class or Nested class from the experts and try using Nested class in java web development.

Author Bio:

This article has been shared by Ethan Millar, working with Aegis SoftTech as technical writer from last 5 years. He especially write articles for Java, .Net, CRM and Hadoop. The objective to write this article is to Define and Use Java Inner Class or Nested Class in Web Development. You can find Ethan Millar here in Facebook and Google Plus

Stanislaus Okwor is a Web Designer / Developer based in Lagos - Nigeria. He is the Director at Stanrich Online Technologies. He is knowledgeable in Content management System - Wordpress, Joomla and PHP/MySQL etc

Leave a Reply

WhatsApp chat
Verified by MonsterInsights