Java Program to Check if a Given Class is a Local Inner Class
Last Updated :
06 Oct, 2021
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
// 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());
}
}
OutputIs 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
// 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
}
}
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
// 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());
}
}
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
// 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());
}
}
OutputCanonical 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
// 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);
}
}
OutputIs gfg a local class instance?: true
Similar Reads
Java Program to Check if a Given Class is an Inner Class 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 classMethod Local inner classesAnonymous inner classesStati
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
Local Variable Type Inference or LVTI in Java 10 In Java, type inference refers to the automatic detection of the datatype of a variable, done generally at compiler time. In this article, we are going to discuss about local variable type inference in Java in detail.What is Local Variable Type Inference (LVTI)?Local variable type inference is a fea
5 min read
How to Find User Defined Objects From LinkedHashSet in Java? LinkedHashSet is used to store elements in which order they were inserted. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements in the order in which they were i
3 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