Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

object

oop

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 insideCat;

    public Cat(){
         System.out.println("Cat constructor");
         insideCat = new Cat();
    }

}

But what if I don't create a new Cat object within the constructor, but take a parameter of Cat type and assign it to the insideCat field.

public class Cat {
    String name="default name";
    Cat insideCat;

    public Cat(Cat insideCat){
         System.out.println("Cat constructor");
         this.insideCat = insideCat;
    }

}

I am just playing around with the code, just trying to find out what Java can do and cannot. In the second code, everything looked normal, until I started to test this class. I need a Cat object to create a Cat object (and to create this Cat object I need another Cat object...and it goes on). So technically I cannot test this class.

So my question is WHY does java allow to create an instance variable of its own type? I guess the whole purpose of a constructor is to initialize it's instance variables. So either I have to create a new object to initialize the insideCat or else I have to take Cat object from outside. Both doesn't seem to work.

What am I missing here. Is there any occurrence where instance variables of its own types can become useful, and can be used without any problem? Is it bad OOP practice to come up with classes like this?

like image 755
DesirePRG Avatar asked Apr 09 '15 18:04

DesirePRG


People also ask

Can a data field in a class can be of an object type?

Yes, they can. anything can be a field.

Can a class have an object of itself?

A class declaration can contain static object of self type, it can also have pointer to self type, but it cannot have a non-static object of self type.

Why the fields of a class should be private?

Fields should be declared private unless there is a good reason for not doing so. One of the guiding principles of lasting value in programming is "Minimize ripple effects by keeping secrets." When a field is private , the caller cannot usually get inappropriate direct access to the field.

What are the fields of a class?

A field is a variable of any type that is declared directly in a class or struct. Fields are members of their containing type. A class or struct may have instance fields, static fields, or both. Instance fields are specific to an instance of a type.


1 Answers

Classes like this exist all the time.

Consider linked lists or trees, e.g.,

class ListNode {
  ListNode next;
  // Etc.
}

class TreeNode {
  TreeNode left;
  TreeNode right;
  // Etc.
}

You wouldn't initialize the "child" objects in the constructor, you'd add them later.

In your example you'd need to have a method that created the insideCat at a later time. In general you wouldn't create child objects that had the exact same state, there'd be something to differentiate them either at construction time, in which case you could have a "oh god stop creating these now" condition, or while they were being added, e.g., you'd add them via a method and not in a constructor.

like image 188
Dave Newton Avatar answered Sep 30 '22 06:09

Dave Newton