Get the fully-qualified name of an inner class in Java

Java 8Object Oriented ProgrammingProgramming

A fully-qualified class name in Java contains the package that the class originated from. Also, an inner class is a class that is another class member. So, the fully-qualified name of an inner class can be obtained using the getName() method.

A program that demonstrates this is given as follows −

Example

 Live Demo

public class Main {
   public static void main(String[] argv) throws Exception {
      Class c = java.lang.Character.Subset.class;
      String innerClassName = c.getName();
      System.out.println("The fully qualified name of the inner class is: " + innerClassName);
   }
}

Output

The fully qualified name of the inner class is: java.lang.Character$Subset

Now let us understand the above program.

The getName() method is used to get the fully-qualified name of the inner class i.e. the Subset class and this is stored in className. Then this is displayed. A code snippet which demonstrates this is as follows −

Class c = java.lang.Character.Subset.class;
String innerClassName = c.getName();
System.out.println("The fully qualified name of the inner class is: " + innerClassName);
raja
Published on 09-Jan-2019 07:28:26
Advertisements