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
// 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 {
}
}
OutputIs 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));
}
}
OutputIs 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
// 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);
}
}
OutputIs gfg an inner class object? : true
Similar Reads
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 clas
5 min read
Java Program to Check if a Given Class is an Anonymous Class Anonymous classes are the inner classes with no name. We can't create the instances of this anonymous class since they have no name. Anonymous inner class are mainly created in two ways: Class (may be abstract or concrete)Interface Syntax: The syntax of an anonymous class expression is like the invo
5 min read
Class Type Casting in Java Typecasting is the assessment of the value of one primitive data type to another type. In java, there are two types of casting namely upcasting and downcasting as follows: Upcasting is casting a subtype to a super type in an upward direction to the inheritance tree. It is an automatic procedure for
4 min read
Java Program to Convert String to Object In-built Object class is the parent class of all the classes i.e each class is internally a child class of the Object class. So we can directly assign a string to an object. Basically, there are two methods to convert String to Object. Below is the conversion of string to object using both of the me
2 min read
How to Check the Accessibility of the Static and Non-Static Variables by a Static Method? The static keyword is a non - access modifier in Java which can be used for variables, methods, and block of code. Static variables in Java belong to the class i.e it is initialized only once at the start of the execution. By using static variables a single copy is shared among all the instances of
3 min read
Shadowing in Java Inner class means one class that is a member of another class. There are basically four types of inner classes in java. Nested Inner class can access any private instance variable of the outer class. Like any other instance variable, we can have access modifier private, protected, public, and defaul
3 min read