Related Articles

Related Articles

Java Program to Check if a Given Class is a Local Inner Class
  • Last Updated : 17 Nov, 2020

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:

  • To check whether the class is a local inner class, java.lang.Class provides a method called isLocalClass().
  • This method returns true if the given class is a local class and false otherwise. Note that “isLocalClass()” is defined in java.lang.Class class. So, you need to first call the “getClass()” method of the object.
  • This method returns the Class instance representing the class of the object. See the following code —

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output

Is inner a local class object? :true

Approach 2:



  • This approach is about checking the canonical name of the class and then deciding whether it is a local class or not.
  • The getCanonicalName() method defined in java.lang.
  • Class class, returns the canonical name of the class in as a string.
  • For example, if the name of a class represented by the java.lang.Class object, is “GFG”, then the “getCanonicalName()” method returns “GFG”.
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”.

  • But this is not the case for local, anonymous classes.
  • If the class is a member class, say, “GFG” is a member class of “GeeksForGeeks”, then “getCanonicalName()” will return “GeeksForGeeks.GFG”(in the form “(Name_of_Enclosing_Class).(Name_of_this_Class)”).

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check what the method returns
// when class is an annymous 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
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// java program to check what the method returns
// when class is an annymous 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());
    }
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

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

chevron_right


Output

Is gfg a local class instance?: true

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :