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


Advertisements

Description

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

Declaration

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

public static boolean isSynchronized(int mod)

Parameters

mod − a set of modifiers.

Returns

true if mod includes the synchronized modifier; false otherwise.

Example

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

package com.tutorialspoint;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class ModifierDemo {
   public static void main(String[] args) throws NoSuchFieldException, SecurityException {
      Method [] methods = SampleClass.class.getMethods();
      System.out.println(Modifier.isSynchronized((methods[0].getModifiers())));
   }
}

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