Java Program to Get the List of Defined Classes and Interfaces in a Class

You are currently viewing Java Program to Get the List of Defined Classes and Interfaces in a Class

Java program to get the list of defined classes and interfaces in a class

// Java program to get the list of defined classes 
// and interfaces in a class

import java.lang.reflect.Field;
public class Main {
  public interface Inf {}

  public class Sample implements Inf {}

  public static void main(String[] args) throws ClassNotFoundException {
    Class cls = Main.class;
    Class[] classes = cls.getClasses();

    System.out.println("Classes and Interfaces defined in Main class: ");
    for (Class cl: classes)
      System.out.println(cl);
  }
}

Output

Classes and Interfaces defined in Main class: 
class Main$Sample
interface Main$Inf

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply