Java Reflection constructor get modifiers

Introduction

Java reflection provides APIs to find constructors of a class.

The following program prints the list of public constructors of the specified class:

import java.lang.reflect.Constructor;
import java.lang.reflect.Modifier;

public class Main {
   public static void main(String args[]) throws Exception {
      Class c = Class.forName("java.lang.String");
      Constructor[] constructors = c.getDeclaredConstructors();
      System.out.println("Public Construtors of :");
      for (Constructor con : constructors) {
         if (Modifier.isPublic(con.getModifiers())) {
            System.out.println(con);
         }//  ww w  . ja va2  s  . c o m
      }
   }
}



PreviousNext

Related