tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Reflection > Get Class Type

Get Class Type 

The following example shows how to verify whether the given class is an interface, enum or class. Class provides the following APIs,

public boolean isInterface()
 - Returns true if the class object is an interface otherwise false.

public boolean isEnum()
 - Returns true if the class object is an enum type otherwise false.


File Name  :  
com/bethecoder/tutorials/reflection/GetClassType.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.reflection;

import java.io.Serializable;
import java.util.Stack;

public class GetClassType {

  enum ServerStatus {
    STARTING,
    RUNNING,
    DOWN
  }
  
  /**
   @param args
   */
  public static void main(String[] args) {
    checkClassOrInterface(String.class);
    checkClassOrInterface(Stack.class);
    
    System.out.println();
    checkClassOrInterface(Runnable.class);
    checkClassOrInterface(Serializable.class);
    
    System.out.println();
    checkClassOrInterface(Thread.State.class);
    checkClassOrInterface(ServerStatus.class);
  }
  
  private static void checkClassOrInterface(Class<?> clazz) {
    if (clazz.isInterface()) {
      System.out.println(clazz + " is an Interface.");
    else if (clazz.isEnum()) {
      System.out.println(clazz + " is an Enum.");
    else {
      System.out.println(clazz + " is a Class.");
    
  }

}
   

It gives the following output,
class java.lang.String is a Class.
class java.util.Stack is a Class.

interface java.lang.Runnable is an Interface.
interface java.io.Serializable is an Interface.

class java.lang.Thread$State is an Enum.
class com.bethecoder.tutorials.reflection.GetClassType$ServerStatus is an Enum.



 
  


  
bl  br