paul
paul

Reputation: 4487

Check what java class type is this

I want to check String is interface or not using Reflection method .isInterface. Here is what I tried but it gives Class not found exception.

public class CheckingClassType {
    public static void main(String args[]) {
        try {
            Class c = Class.forName("String");
            System.out.println(c.isInterface());

        } catch (Exception e) {
            System.out.println(e);
        }

    }
}

Upvotes: 0

Views: 44

Answers (1)

Karthik R
Karthik R

Reputation: 5786

Use Class c = Class.forName("java.lang.String");

You need to give the full package name of the class. The reflection needs to know that to instantiate the class name as multiple classes with same name can exist in different packages.

Upvotes: 3

Related Questions