Java Program to Check if a given Class is a Local Inner Class


The nested class that is defined inside the body of a method or within a loop like for and if, is called as local inner class.

Before making a java program to check whether a given class is a local inner class or not we need to understand the concept of nested class. Let’s discuss this in detail.

Nested Class

When we create class within another class, it is referred as nested class. The nested classes share the same scope. It allows the grouping of similar classes and it increases the encapsulation (encapsulation means wrapping of similar functionalities in a single unit). In java, we know that a class could not be defined using private access specifier but we can define nested class as private. There are two kinds of nested class available: static and non-static nested class.

The static nested class is defined using static modifier but Non-static nested class is not defined using static keyword and it is also known as the inner class.

Inner Class

Non-static or Inner class can access all the static and non-static methods and member variables of its outer class. It can also access the members that are private but, an outer class cannot access the private members of inner class. Inner class is the most used type of nested class. We will discuss its type later in this article.

Syntax

class Class_otr {
   // Outer class body
   class Class_inn { 
      // Inner class body
   }
}

To create class we have used ‘class’ keyword. ‘Class_otr’ is the name of outer class and ‘Class_inn’ is the name of inner class.

Example

The following program illustrates the inner class.

class Cart {
   // the outer class for class ‘Bill’
   private String item1 = "Rice";
   private class Bill {
      public void info() {
         System.out.println("This is an inner class");
       
         // accessing private member ‘item1’
         System.out.println("Item 1 in Cart is: " + item1);
      }
   }
   void display() {
      Bill ob = new Bill(); 
     
      // to create object of class Bill
      ob.info(); 
     
      // method calling using object ‘ob’
   }
}
public class Tutorialspoint {
   public static void main(String args[]) {
      Cart obj = new Cart(); 
     
      // to create object of class Cart
      obj.display(); 
     
      // method calling of outer class Cart using its object
   }
}

Output

This is an inner class
Item 1 in Cart is: Rice

In the above program, the class ‘Cart’ is outer class of ‘Bill’. We have created a method ‘info’ that can access the private member ‘item1’ of outer class. Notice that ‘Bill’ is a private class and we have created its object inside the method ‘display()’ of outer class. The object of ‘Cart’ is created inside the main method. When we called the ‘display()’ method it has automatically called the members of class ‘Bill’. Hence we got that output.

Program for Checking Local Inner Class

There are two main types of inner class −

  • Local Inner Class − It is defined inside a method block. We can also define it in the body of loops. Just like the local variables and methods, its scope lies within the method or loop block where it is defined.

  • Anonymous Inner Class − It is the type of inner class that has no name.

We will use the following inbuilt methods of java to check whether specified class is local inner class or not.

  • getClass() − It returns the class of specified object.

  • getEnclosingClass() − It returns the enclosing class if any local or anonymous class is present otherwise it returns null.

  • isLocalClass() − It reutrns true if class of specified object is local class otherwise false.

To work with above discussed method we need to import the package mentioned below −

import java.lang.*; 

Example

import java.lang.*;
public class Otr { 
   // It is the outer class
   public static void main(String[] args){
      class LocalInner{
         public void show(){
            System.out.println("I am local inner class");
            System.out.println("Name of Outer Class of LocalInner class is: " + getClass().getEnclosingClass()); 
            // to fetch the name of its outer class
         }
      }
      LocalInner ob= new LocalInner();
      // Creating object of ‘Inner’ class
      ob.show();
      // fetching the name of class of object ‘ob’ i.e. LocalInner
      Class cls = ob.getClass();
      // Checking if class ‘LocalInner’ is local class or not
      boolean isLocal = cls.isLocalClass();
      // Printing the Boolean result
      System.out.println("Is LocalInner a local inner class: " + isLocal);
   }
} 

Output

I am local inner class
Name of Outer Class of LocalInner class is: class Otr
Is LocalInner a local inner class: true

Since the ‘isLocalClass()’ method returning true and we are getting the name of outer class too. The ‘LocalInner’ class is a local inner class.

Conclusion

In this article, we have discussed a java program to check if a given class is Local inner class. In our program we have used different inbuilt methods of java.lang package. We have also understood the concept of nested and inner classes in java.

Updated on: 02-May-2023

117 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements