Open In App

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

Local Inner Classes are classes declared inside a block. These classes are only visible inside the block. So, You need to instantiate the class within the block. Sometimes, this block can be for loop or if-else clause. These classes can access the fields of a class enclosing it. The local inner class must be instantiated in the block they are defined in.

We will discuss using the below approaches how to check if a given class is a Local Inner Class. 



Approach 1:




// Java program to check if a class is inner
// local class using isLocalClass() method
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        class LocalInnerClass {
            // This is a local inner class
        }
        // Creating a LocalInnerClass object
        LocalInnerClass inner = new LocalInnerClass();
  
        // Getting the Class instance associated with
        // the class of inner(i.e. LocalInnerClass)
        // which is returned by getClass() method of inner
        Class localClass = inner.getClass();
  
        // isLocalClass() method of localClass returns true
        // if and only if localClass is a Local Class
        System.out.println(
            "Is inner a local class object? :"
            + localClass.isLocalClass());
    }
}

Output

Is inner a local class object? :true

Approach 2:

GFG gfg = new GFG();
System.out.println(gfg.getClass().getCanonicalName());

If we run the above code in a program, then the output received will be:

Output :

GFG

Note: A.)  If there is a package name, then the output will be “package-name.GFG”.




// Java program to check what the method returns
// when class is an anonymous class
  
import java.io.*;
  
class GeeksForGeeks {
    public static void main(String[] args)
    {
        // Creating a GeeksForGeeks object
        // to create a GFG object
        GeeksForGeeks geeks = new GeeksForGeeks();
        // Creating a GFG object
        GFG gfg = geeks.new GFG();
  
        // getClass() returns a java.lang.Class
        // instance representing the GFG class
        System.out.println(
            gfg.getClass().getCanonicalName());
    }
  
    class GFG {
        // This is a member class of GeeksForGeeks
    }
}

Output
GeeksForGeeks.GFG

B.)  If the class is an anonymous class inside a class (say, “GFG”) then the “getCanonicalName()” method returns null because it does not have a canonical name.




// java program to check what the method returns
// when class is an anonymous class
  
import java.io.*;
  
class GFG {
    public static void main(String[] args)
    {
        GFG gfg = new GFG() {
            // This is an Anonymous class
            // that extends the GFG class
        }; // Remember to put the semicolon here
        System.out.println(
            gfg.getClass().getCanonicalName());
    }
}

Output
null

C.)  If the class is a local inner class, suppose, “GFG” is a local class inside “GeeksForGeeks”, then also “getCanonicalName()” will return null. Because, it does not have a canonical name




// Java program to check if a class is a
// local inner class
  
import java.io.*;
  
class GeeksForGeeks {
    public static void main(String[] args)
    {
        class GFG {
            // This is a local class
        }
        class Geeks {
            // This is an another local class
        }
  
        GFG gfg = new GFG();
        Geeks geeks = new Geeks();
  
        System.out.println(
            "Canonical Name of the class of gfg:"
            + gfg.getClass().getCanonicalName());
        System.out.println(
            "Canonical Name of the class of geeks:"
            + geeks.getClass().getCanonicalName());
    }
}

Output
Canonical Name of the class of gfg:null
Canonical Name of the class of geeks:null

Using this canonical name, you can achieve the given task. Check the following code:




// Java program to check if the class
// local inner class
  
import java.io.*;
  
class GeeksForGeeks {
    public static void main(String[] args)
    {
        class GFG {
            // This is a local class
        }
  
        GFG gfg = new GFG();
  
        // Storing the class instance associated with gfg
        // i.e. Class instance representing GFG
        Class classInstance = gfg.getClass();
  
        // Checking whether the classInstance has a
        // canonical name
        boolean hasCanonicalName
            = (classInstance.getCanonicalName() != null);
  
        // If hasCanonicalName is false then it is sure that
        // either it is a local class or anonymous class or
        // an array whose component type does not have a
        // canonical name
  
        // Now check whether it is an anonymous Class
        boolean isAnonymousClass
            = classInstance.isAnonymousClass();
  
        // Check if it is an array
        boolean isArray = classInstance.isArray();
  
        // If all the three isArray,isAnonymousClass and
        // hasCanonicalName are false then the classInstance
        // is surely a local class
        boolean isLocalClass
            = (!hasCanonicalName && !isAnonymousClass
               && !isArray);
        System.out.println(
            "Is gfg a local class instance?: "
            + isLocalClass);
    }
}

Output
Is gfg a local class instance?: true

Article Tags :