Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Check if a Given Class is a Local Inner Class in Java



The nested class that is defined inside the body of a method or within a loop, like for and if, is called a 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 classes first. Let's discuss this in detail.

Nested Class

When we create a class within another class, it is referred to as a nested class. The nested classes share the same scope. It allows the grouping of similar classes and encapsulation (wrapping of similar functionalities in a single unit). In Java, we know that a class cannot be defined using a private access specifier, but we can define a nested class as private.

There are two kinds of nested classes available:

  • Static Nested Class: The static nested class is defined using static modifier.

  • Non-static Nested Class: The non-static nested class is not defined using the 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 the inner class. Inner class is the most used type of nested class.

Syntax

Following is the syntax of creating an inner class in Java -

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

To create a class, we have used the 'class' keyword. 'Class_otr' is the name of the outer class, and 'Class_inn' is the name of the 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

Program for Checking Local Inner Class

There are two main types of inner classes:

  • 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 a specified class is a local inner class or not.

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

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

  • isLocalClass(): It returns TRUE if a class of the specified object is a local class, otherwise FALSE.

Example

Let's see a Java program which uses the getClass(), getEnclosingClass(), and isLocalClass() methods together to check a local inner class.

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);
   }
} 

Since LocalInner class is a local inner class, the program will return TRUE.

I am local inner class
Name of Outer Class of LocalInner class is: class Otr
Is LocalInner a local inner class: true
Updated on: 2025-05-30T17:47:00+05:30

229 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements