Java Class isSynthetic() Method



Description

The Java Class isSynthetic() method returns true if this class is a synthetic class, else returns false.

Declaration

Following is the declaration for java.lang.Class.isSynthetic() method

public boolean isSynthetic()

Parameters

NA

Return Value

This method returns true if and only if this class is a synthetic class as defined by the Java Language Specification.

Exception

NA

Checking a Class to be Synthetic Example

The following example shows the usage of java.lang.Class.isSynthetic() method. In this program, we've created an instance of ClassDemo and then using getClass() method, the class of the instance is retrieved and using isSynthetic() method, we've checked the status and result is printed.

package com.tutorialspoint;

public class ClassDemo {

   public static void main(String[] args) {

      ClassDemo c = new ClassDemo();
      Class cls = c.getClass();

      // returns true if this class is a synthetic class, else false
      boolean retval = cls.isSynthetic();
      System.out.println("It is a synthetic class ? " + retval);        
   }
} 

Output

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

It is a synthetic class ? false

Checking an ArrayList to be Synthetic Example

The following example shows the usage of java.lang.Class.isSynthetic() method. In this program, we've used class of ArrayList and using isSynthetic() method, we've checked the status and result is printed.

package com.tutorialspoint;

import java.util.ArrayList;

public class ClassDemo {

   public static void main(String[] args) {

      ClassDemo c = new ClassDemo();
      Class cls = ArrayList.class;

      // returns true if this class is a synthetic class, else false
      boolean retval = cls.isSynthetic();
      System.out.println("It is a synthetic class ? " + retval);        
   }
} 

Output

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

It is a synthetic class ? false

Checking a Thread to be Synthetic Example

The following example shows the usage of java.lang.Class.isSynthetic() method. In this program, we've used class of Thread and using isSynthetic() method, we've checked the status and result is printed.

package com.tutorialspoint;

public class ClassDemo {

   public static void main(String[] args) {

      ClassDemo c = new ClassDemo();
      Class cls = Thread.class;

      // returns true if this class is a synthetic class, else false
      boolean retval = cls.isSynthetic();
      System.out.println("It is a synthetic class ? " + retval);        
   }
} 

Output

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

It is a synthetic class ? false
java_lang_class.htm
Advertisements