java.lang.reflect.Modifier.isPublic() Method Example


Advertisements

Description

The java.lang.reflect.Modifier.isPublic(int mod) method return true if the integer argument includes the public modifier, false otherwise.

Declaration

Following is the declaration for java.lang.reflect.Modifier.isPublic(int mod) method.

public static boolean isPublic(int mod)

Parameters

mod − a set of modifiers.

Returns

true if mod includes the public modifier; false otherwise.

Example

The following example shows the usage of java.lang.reflect.Modifier.isPublic(int mod) method.

package com.tutorialspoint;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

public class ModifierDemo {
   public static void main(String[] args) throws NoSuchFieldException, SecurityException {
      Field field = SampleClass.class.getDeclaredField("sampleField");
      System.out.println(Modifier.isPublic(field.getModifiers()));
   }
}

class SampleClass {
   public String sampleField;
   
   public final 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 −

true
java_reflect_modifier.htm
Advertisements