Look at this line below:
targetClass = Class.forName(className).newInstance().getClass();
My question here is why does he call newInstance().getClass();
Would it be enough to write
targetClass = Class.forName(className);
?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is no sense to call
newInstance().getClass(), because it will yield the sameClassinstance where it was instantiated from.Rather, it creates an extra instance, that will be “dropped” immediately. Even it will not work, if the class does not have a default constructor.
Also, the cost of the instantiation, initialisation, etc. may be remarkable.