Skip to content
Advertisement

Generics and reflection in Java. Classes

I have a task where I have to implement method of creating object of the given class.

JavaScript

How should I implement this method correctly? I tried to do it in this way

JavaScript

But it always throws NoSuchMethodException. Moreover, from the task “It’s guaranteed that all subclasses of Bakery will have public parameterless constructor.” And InvocationTargetException is not imported in the task so it should be implemented without it. What’s the problem?

Advertisement

Answer

This is how to do it. You have to use getDeclaredConstructor()

JavaScript

Usage

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement