mercredi 3 mars 2021

How to use newInstance() ? JAVA

Im trying to use newInstance in different ways but I cant figure out which is the right one.

Child c = new Child();
Constructor[] ctors = c.getClass().getDeclaredConstructors();
Constructor ctor = ctors[0];

// I tried this:
Child c = (Child)ctor.newInstance(); // just to see if newInstance would work, but nothing

I did this (above) to get my constructor, but I already tried this as I saw in some posts:

c.getClass().getDeclaredConstructors().newInstance();

Which is the right way to use newInstance? Im very confused about this.





Aucun commentaire:

Enregistrer un commentaire