Is it okay for a class to have a field of its own type

后端 未结 4 807
暗喜
暗喜 2021-01-18 12:22

I did the following, i got a stackoverflow error after some time, and I understand why it was.

public class Cat {
    String name=\"default name\";
    Cat i         


        
4条回答
  •  南笙
    南笙 (楼主)
    2021-01-18 13:07

    There is nothing wrong in having an instance member of same class. an Employee has a manager and the manager is also an Employee

     public class Employee{
         private Employee manager;
         //getters setters and constructor
     }
    

提交回复
热议问题