Can a class have an reference variable of declared type of that class?? This assignment seems to say "yes", but that is really surprising. private Human father; and private Human mother; I thought well maybe codegym.cc wants us to make father and mother static, but then that would not work with the toString they gave us...because of this.father and this.mother Except I Googled and the answer seems to be "yes" https://stackoverflow.com/questions/49593877/can-a-java-class-have-an-instance-of-type-itself There were other such results on Google of: "java can a class contain an instance of itself" Anybody else surprised?
can a class have an reference variable of declared type of that class
You must be signed in to leave a comment
yz Backend Developer
29 November 2019, 08:51
first time it looks confusing but u ll get it just continue studying if u dont get something just pass it dont force urself later u gonna come back to it and realize how it works
Kent Hervey Software Engineer/Consult at Zeal IT Consultants
23 November 2019, 19:52
It bothers me that nowhere have I told Java that father and mother are strings...not integers or whatever: private Human father; private Human mother;
23 November 2019, 16:18
You did answer your question, of course it can contain a field that is of the same type as the class. When objects are created, they are independent from each other. This is how OOP works, otherwise you would need to make separate classes for every single object in code instead of just reusing one class for multiple situations. In this task you would have to make a grandfather class, a grandmother class, a father class, a mother class, a child1 class, a child2 class, a child3 class. If this was the case imagine if you were tasked to create a database of everyone in a country.