Class Containing Reference of Itself | Sololearn: Learn to code for FREE!
+ 23

Class Containing Reference of Itself

Why does Java allow having a reference of the class itself as its own class member, when it is prone to failure? A simple code like this one throws a StackOverflowError. public class Program { int value = 1; // regardless of whether or not this is static Program a; Program() { a = new Program(); } public static void main(String[] args) { Program b = new Program(); System.out.print(b.a.value); } } It seems contradicting to the nature of the language itself when Java compilers seemingly check most things and wouldn't even compile while (false). Of course, we can remove the initialization in the class constructor, but what is the point of allowing a reference of a class to be its own member in the first place?

12th Dec 2017, 12:46 PM
Hatsy Rei
Hatsy Rei - avatar
11 Answers
+ 15
The error is due to your constructor recreating a new instance of itself, thus repeating the creation process again … until it does get terminated like that, or runs out of memory to use.
13th Dec 2017, 5:25 PM
Katie (Ctrl-Alt-Cuteness)
Katie (Ctrl-Alt-Cuteness) - avatar
+ 6
Well that is why I miss pointers in Java. Lets take a look at Singleton pattern (for those who don't know: it's used to allow only one instance per class). I cannot write a code really, cause I'm in class, no pun intended, so I will just share links I found useful about it: https://www.javaworld.com/article/2073352/core-java/simply-singleton.html Additionaly it's used for example in linked list where current element of a list is pointing/refering to next one. (Yes I know it's built in Java already, but what stops you from writing it yourself, right? :D)
12th Dec 2017, 1:49 PM
Jakub Stasiak
Jakub Stasiak - avatar
+ 4
I wouldn't use a self reference inside your object as this kind of playing in the code leads to bugs and errors and they are very hard to detect. Unless your'e using some program as help, like checkmarx, it is going to be really hard to do that. So think about it. Good luck.
14th Dec 2017, 3:03 PM
Ben hart
+ 3
Now that I think of actually typing it, and that @~ swim~ mentioned the term, one could use it to create a memory-hogging fibonacci generator… Edit: As long as there is a specified "stop" condition which gets reached…
16th Dec 2017, 5:16 PM
Katie (Ctrl-Alt-Cuteness)
Katie (Ctrl-Alt-Cuteness) - avatar
+ 3
I think there is no need to write__b.a.value Instead we can write either a.value or b. value
26th Dec 2017, 1:11 PM
Nashat
Nashat - avatar
+ 2
I use it for tree data structures. I use thoose references for children of the class
13th Dec 2017, 5:03 PM
Daniel S
Daniel S - avatar
+ 2
@Katie (Ctrl-Alt-Cuteness) Sounds pretty interesting.
16th Dec 2017, 5:19 PM
Jacob Duelz
Jacob Duelz - avatar
+ 2
programming
21st Dec 2017, 12:54 AM
Jordan Timmons
0
Maybe the reason why you can make an object of the main class is to easily call other methods if there are very many (although "this.*" would work better). Otherwise, it might be an error that wasn't caught when Java was first made, but I doubt this. (I'm not very skilled in the area concerning stacks, buffers, etc., so this is my theory. Hope that this is of some help.) It appears to be similar to this situation: "If there is a set that contains all sets, does that set contain itself?" If you think about it for too long, it starts to feel like going down an infinite tunnel, and, eventually, you will stop thinking about it because it goes nowhere.
13th Dec 2017, 11:25 PM
Jacob Duelz
Jacob Duelz - avatar
- 1
Це дозволено так як клас має, а не власне тіло лише силку а силка це просто адреса.
17th Dec 2017, 11:50 AM
Віталій Білий
Віталій Білий - avatar
- 4
hii hatsy rei you are really good in this field and sorry i cant answer you about this post of your . i intersted in know that how long you are on sololearn and have you completed all your coding skills in sololearn only or you are learning from anyother platfrom also i would very if you will guide me i am new and wanted to work hard on this field 😀😀
13th Dec 2017, 9:34 AM
Shivam Raj
Shivam Raj - avatar