Java newInstance


Java newInstance

With the Class object, following operations can be performed. There are three methods in the Class.

1. public native boolean isInterface();
2. public native boolean isArray();
3. public native boolean isPrimitive();

The above methods are used in the following program.

interface Test {  }
public class Student
{
  public static void main(String args[]) throws InstantiationException, IllegalAccessException, ClassNotFoundException
  {

    Student std1 = new Student();
    Class cls1 = std1.getClass();

    System.out.println("\nStudent class name: " + cls1.getName());
    System.out.println("Student is interface: " + cls1.isInterface());
    System.out.println("Student is array: " + cls1.isArray());

    int marks[] = {50, 60, 70 };
    Class cls2 = marks.getClass();
    System.out.println("\nmarks is array: " + cls2.isArray());
    
    Class cls3 = Class.forName("Test");
    System.out.println("\nTest is interface: " + cls3.isInterface());
   }
}

Java newInstance

With the methods isInterface() and isArray(), we can check a class object represents an interface or an Array.

View All for Java Differences on 90 Topics

Leave a Comment

Your email address will not be published.