-1

So i'm trying to learn to use the Reflaction subject in java(so i think its called), so i did a little project to try and see how to make an object without the optimal Constructor pattern with the "new" word.

unfortunately, it shows me an error about the making the class types array for the contructor. here is my project:

SomeClass.java:

public class SomeClass {

public static void main(String[] args) {

    ArrayList<Class> classes = new ArrayList<>();
    classes.add(Integer.class);
    classes.add(String.class);
    classes.add(Boolean.class);
    Class[] classesArray = (Class[]) classes.toArray(); //here is where it showes the error

    ArrayList<Object> objects = new ArrayList<>();
    objects.add(2452);
    objects.add("sfhfshsf");
    objects.add(true);
    Object[] studentObjects = objects.toArray();

    Student student = null;
    try {
        student = Student.class.getConstructor(classesArray).newInstance(
                studentObjects);
    } catch (InstantiationException | IllegalAccessException
            | IllegalArgumentException | InvocationTargetException
            | NoSuchMethodException | SecurityException e1) {
        e1.printStackTrace();
    }

    System.out.println(student);

}

}

Student.java:

public class Student {
int studendID = 0;
String studentName = "";
boolean isSome1 = false;

public Student() {
}

public Student(int studendID, String studentName, boolean isSome1) {
    this.studendID = studendID;
    this.studentName = studentName;
    this.isSome1 = isSome1;
}

@Override
public String toString() {
    return "Student [studendID=" + studendID + ", studentName="
            + studentName + ", isSome1=" + isSome1;
}

}

the error:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Class;
at SomeClass.SomeClass.main(SomeClass.java:16)

if i'm doing it wrong, so what is the right way? help please.

liranahum
  • 143
  • 3
  • 13
  • You cannot cast array of one type to array of another - problem is addressed here [http://stackoverflow.com/questions/1115230/casting-object-array-to-integer-array-error](http://stackoverflow.com/questions/1115230/casting-object-array-to-integer-array-error) – xersiee Jan 03 '16 at 14:34
  • This is not a duplicate, because the bulk of the problem is not in converting a list to an array, but in what goes onto the list in the first place. – Sergey Kalinichenko Jan 03 '16 at 14:35
  • 2
    In this case, rather than converting a List to an array, just create the array directly: `Class>[] classes = { Integer.class, String.class, Boolean.class }; – WaelJ Jan 03 '16 at 14:35

3 Answers3

3

You get an error because toArray returns an array of Objects. There is a fix for that, but there is an easier way to construct an array of Class objects:

Class[] classesArray = new Class[] {
    Integer.TYPE, String.class, Boolean.TYPE
};

Note the use of .TYPE instead of .class for Integer and Boolean. This is because your constructor takes primitive int and boolean, not Integer and Boolean.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399
  • yes, you were right! after adding "new Class[0]" in my "toArray()" function, i got another error, and changing to TYPE instead of class, did help me. thanks! – liranahum Jan 03 '16 at 15:37
2

You can use varargs to simplify this.

public static void main(String[] args) throws Exception {
    Student student = Student.class
                             .getConstructor(Integer.class, String.class, Boolean.class)
                             .newInstance(2452, "sfhfshsf", true);

    System.out.println(student);
}

The problem you were having was that toArray() only returns an Object[] not a Class[] and it cann't be just cast to one.

What you could have done was.

Class[] classesArray = (Class[]) classes.toArray(new Class[0]); 
Peter Lawrey
  • 498,481
  • 72
  • 700
  • 1,075
0

Your Student class does not have a constructor that accepts a Class[], hence the exception.

You should use either

 Student student = Student.class.getConstructor().newInstance();//calling no-args constructor

 or

 Student student = Student.class.getConstructor(Integer.class, String.class, Boolean.class).newInstance(10, "Mark', true);
dsp_user
  • 1,625
  • 2
  • 12
  • 21