Dealing with a private constructor

Report a typo

Here you need to apply your knowledge of reflection to create an instance of a class with private constructors. The class has two fields, two constructors and two getters:

class Person {
    private String name = "unknown";
    private int age;

    private Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    private Person(int age) {
        this.age = age;
    }

    // getters
}

Implement the createPerson method that must return an object of the Person class based on the passed name and age.

Write a program in Java 17
class InstanceUtils {

public static Person createPerson(String name, int age) {
return null;
}
}
___

Create a free account to access the full topic