java.lang.reflect.Constructor.getModifiers() Method Example


Advertisements

Description

The java.lang.reflect.Constructor.getModifiers() method returns the Java language modifiers for the constructor represented by this Constructor object, as an integer. The Modifier class should be used to decode the modifiers.

Declaration

Following is the declaration for java.lang.reflect.Constructor.getModifiers() method.

public int getModifiers()

Returns

the Java language modifiers for the underlying member.

Example

The following example shows the usage of java.lang.reflect.Constructor.getModifiers() method.

package com.tutorialspoint;

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

public class ConstructorDemo {

   public static void main(String[] args) {

      Constructor[] constructors = SampleClass.class.getDeclaredConstructors();
      for (Constructor constructor : constructors) {
         System.out.println("Modifier: " + Modifier.toString(constructor.getModifiers()));
         System.out.println("Constructor: " + constructor.toGenericString());
      }
   }
}

class SampleClass {
   private String sampleField;

   public SampleClass() throws ArrayIndexOutOfBoundsException {
   }

   private SampleClass(String sampleField){
      this.sampleField = sampleField;
   }

   public String getSampleField() {
      return sampleField;
   }

   public void setSampleField(String sampleField) {
      this.sampleField = sampleField; 
   } 
}

Let us compile and run the above program, this will produce the following result −

Modifier: public
Constructor: public com.tutorialspoint.SampleClass() throws java.lang.ArrayIndexOutOfBoundsException
Modifier: private
Constructor: private com.tutorialspoint.SampleClass(java.lang.String)
java_reflect_constructor.htm
Advertisements