What is it called when an object has an object of the same type inside of itself?
Example:
public class Foo{
public Foo myFoo;
}
What is it called when an object has an object of the same type inside of itself?
Example:
public class Foo{
public Foo myFoo;
}
I don't think there's any specific name for this. Although this concept is used in many different common programming constructs. For instance, when representing a graph, tree, or linked list, the nodes usually have references to other nodes that they are linked/connected to.
It means that Foo
is a 'recursive data structure'. Examples of this are trees, graphs, linked lists, etc. There aren't many significant programs written that don't use at least some recursive structures, e.g. in any SQL server implementation it's pretty common that the query plan that gets executed will be defined in a similar way. As a tiny example, the WHERE
clause might get translated to a FilterNode
that acts on data received from some other Node
(like a table scan):
public interface Node { }
public class FilterNode implements Node {
public Node underlyingNode;
public Condition filterCondition;
}
In many cases the overall structure forms a directed acyclic graph, which means it's easy to safely traverse it recursively. But if it's got cycles then you need to be careful that you don't get into infinite recursion (which is what another answer above is humorously warning about).