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:
- Nested Inner class
- Method Local inner classes
- Anonymous inner classes
- Static nested classes
Approach 1:
- First check if the given class is a nested class
- 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.
- 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.
- 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
import java.lang.reflect.Modifier;
class GFG {
public static void main(String[] args)
{
GFG gfg = new GFG();
InnerClass innerClass = gfg. new InnerClass();
StaticNestedClass statNested
= new StaticNestedClass();
Class classInstance1 = innerClass.getClass();
Class classInstance2 = statNested.getClass();
boolean isNested1
= classInstance1.getEnclosingClass() != null ;
boolean isNested2
= classInstance2.getEnclosingClass() != null ;
boolean isStatic1 = Modifier.isStatic(
classInstance1.getModifiers());
boolean isStatic2 = Modifier.isStatic(
classInstance2.getModifiers());
System.out.println(
"Is innerClass an inner class object? : "
+ (isNested1 && !isStatic1));
System.out.println(
"Is statNested an inner class object? : "
+ (isNested2 && !isStatic2));
}
class InnerClass {
}
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
import java.lang.reflect.Modifier;
class GFG {
public static void main(String[] args)
{
GFG gfg = new GFG() {
};
class LocalInnerClass {
}
LocalInnerClass innerClass = new LocalInnerClass();
Class classInstance1 = gfg.getClass();
Class classInstance2 = innerClass.getClass();
boolean isNested1
= classInstance1.getEnclosingClass() != null ;
boolean isNested2
= classInstance2.getEnclosingClass() != null ;
boolean isStatic1 = Modifier.isStatic(
classInstance1.getModifiers());
boolean isStatic2 = Modifier.isStatic(
classInstance2.getModifiers());
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:
- 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.
- If the given class is none of them, then the class is not an inner class.
- If the class is one of them, then check if the class is static or not using the java.lang.reflect.Modifier.isStatic() method.
- 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
import java.lang.reflect.Modifier;
class GFG {
public static void main(String[] args)
{
GFG gfg = new GFG() {
};
Class classInstance = gfg.getClass();
boolean isInnerClass
= (classInstance.isMemberClass()
|| classInstance.isLocalClass()
|| classInstance.isAnonymousClass())
&& !Modifier.isStatic(
classInstance.getModifiers());
System.out.println(
"Is gfg an inner class object? : "
+ isInnerClass);
}
}
|
Output
Is gfg an inner class object? : true
Share your thoughts in the comments
Please Login to comment...