lundi 2 décembre 2019

How to get the object which invoke a constructor from this constructor? [duplicate]

This question already has an answer here:

I would like to upgrade a constructor to get the object which invokes this constructor from this constructor, typically :

class Person {
    private String s = "Tom";

    public void hello() {
        Name name = new Name();
        System.out.println(name.getName());
        // Display : Tom
    }
}

class Name {
    private String s = "";

    public Name() {
        // Get object which instantiate the constructor and overwrite s
    }

    public String getName() {
        return s;
    }
}

Thanks for help :)

PS : I don't want a solution like

public Name(String s) {
    this.s = s;
}




Aucun commentaire:

Enregistrer un commentaire