Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

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

  • Last Updated : 16 Aug, 2021

Inner class is a member of another class which is basically a non-static nested class i.e. if a class is inside another class and is not static, then the class is called is referred to as an inner class.

Types of Inner Classes:

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. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

  1. Nested Inner class
  2. Method Local inner classes
  3. Anonymous inner classes
  4. Static nested classes

Approach 1: 

  1. First check if the given class is a nested class
  2. Further it is checked whether the class is a non-static class.
    • If both conditions are true, then the given class is an inner class.
  3. Now in order to check if the given class is a nested class, java.lang.Class#getEnclosingClass() method is used that returns a Class instance representing the immediately enclosing class of the object.
    • If the class is a top-level class then the method will return null.
    • This means that if the class is not nested then the method returns null.
  4. Further it is checked whether the given class is static using the java.lang.reflect.Modifier.isStatic() method.

Program to check for nested inner classes

Java




// Java Program to Check if a Given Class is  an Inner Class
 
// Importing package where Modifier is defined
import java.lang.reflect.Modifier;
 
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating as GFG class object
        GFG gfg = new GFG();
 
        // Creating a InnerClass object
        InnerClass innerClass = gfg.new InnerClass();
 
        // Creating a Static Nested Class object
        StaticNestedClass statNested
            = new StaticNestedClass();
 
        // getClass() method of an object returns a
        // java.lang.Class instance representing the class
        // of the object
        Class classInstance1 = innerClass.getClass();
        Class classInstance2 = statNested.getClass();
 
        // Checking if the given classes are nested
 
        // getEnclosinglClass() of Class instance returns a
        // class object representing the immediate enclosing
        // class of the instance
 
        boolean isNested1
            = classInstance1.getEnclosingClass() != null;
        // classInstance1() method returns a Class object
 
        // If the specified class is a top-level class, then
        // it would returns null
 
        boolean isNested2
            = classInstance2.getEnclosingClass() != null;
 
        //  Check if the given classes are static
 
        // getModifiers() method of a class returns the
        // modifiers of the class encoded as an integer
 
        // Modifiers.isStatic returns true if the class
        // has a static modifier elsereturns false
        boolean isStatic1 = Modifier.isStatic(
            classInstance1.getModifiers());
        boolean isStatic2 = Modifier.isStatic(
            classInstance2.getModifiers());
 
        // If the given classes are nested and non-static,
        // then given classes are surely inner classes
 
        // Display message
        System.out.println(
            "Is innerClass an inner class object? : "
            + (isNested1 && !isStatic1));
 
        // Display message
        System.out.println(
            "Is statNested an inner class object? : "
            + (isNested2 && !isStatic2));
    }
 
    // Inner Class
    class InnerClass {
    }
 
    // Static nested class
    static class StaticNestedClass {
    }
}
Output



Is innerClass an inner class object? : true
Is statNested an inner class object? : false

Similarly, for local and anonymous inner classes, the above logic returns true.
 

Program to check for local and anonymous inner classes

Java




// Java Program to Check if a
// Given Class is an Inner Class
 
// Importing package where Modifier is defined
import java.lang.reflect.Modifier;
 
class GFG {
 
    public static void main(String[] args)
    {
        // Creating an anonymous class object
        GFG gfg = new GFG() {
            // This is an anonymous class that extends class
            // GFG
        };
 
        class LocalInnerClass {
            // This is a local inner class
        }
 
        // Creating a LocalInnerClass object
        LocalInnerClass innerClass = new LocalInnerClass();
 
        // getClass() method of an object returns a
        // instance representing the class of the object
        Class classInstance1 = gfg.getClass();
        Class classInstance2 = innerClass.getClass();
 
        // Checking if the given classes are nested
 
        // getEnclosinglClass() of Class instance returns a
        // class object representing the immediate enclosing
        // class of the instance
        boolean isNested1
            = classInstance1.getEnclosingClass() != null;
 
        // classInstance1() method returns a Class object
 
        // If the specified class is a top-level class, then
        // it would returns null
        boolean isNested2
            = classInstance2.getEnclosingClass() != null;
 
        // Checking if the given classes are nested
 
        // getModifiers() method of a class returns the
        // modifiers of the class encoded as an integer
        // Modifiers
        boolean isStatic1 = Modifier.isStatic(
            classInstance1.getModifiers());
 
        // isStatic() returns true if the class has a static
        // modifier else returns false
        boolean isStatic2 = Modifier.isStatic(
            classInstance2.getModifiers());
 
        // Now by this end, classes are nested & non-static
        // Then given classes are surely inner classes
 
        // Print the results
        System.out.println(
            "Is gfg an inner class object? : "
            + (isNested1 && !isStatic1));
        System.out.println(
            "Is innerClass an inner class object? : "
            + (isNested2 && !isStatic2));
    }
}

  

Output
Is gfg an inner class object? : true
Is innerClass an inner class object? : true

 Approach 2:

  1. Directly checking whether the class is local, or a member, or an anonymous class using java.lang.Class#isLocalClass(), java.lang.Class#isMemberClass(), java.lang.Class#isAnonymousClass() methods.
  2. If the given class is none of them, then the class is not an inner class.
  3. If the class is one of them, then check if the class is static or not using the java.lang.reflect.Modifier.isStatic() method.
  4. Print the result when all the conditions are properly satisfied 

Note: However, if you just want to check if the class is a nested class, then you can simply use the java.lang.Class#isLocalClass(), java.lang.Class#isMemberClass(), java.lang.Class#isAnonymousClass() methods. If any of them returns true for a given class, then it is certain that the class is a nested class.

Example 

Java




// Java Program to Check if a Given
// Class is an Inner Class
 
// Importing package where Modifier is defined
import java.lang.reflect.Modifier;
 
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating a local inner class object
        GFG gfg = new GFG() {
 
        };
 
        // getClass() method of an object returns a
        // java.lang.CLass
        // instance representing the class of the object
        Class classInstance = gfg.getClass();
 
        // Checking whether Class is -
        // local, member, an anonymous inner class
        // and not static
        boolean isInnerClass
            = (classInstance.isMemberClass()
               || classInstance.isLocalClass()
               || classInstance.isAnonymousClass())
              && !Modifier.isStatic(
                     classInstance.getModifiers());
 
        // Printing the results
        System.out.println(
            "Is gfg an inner class object? : "
            + isInnerClass);
    }
}
Output
Is gfg an inner class object? : true



My Personal Notes arrow_drop_up
Recommended Articles
Page :