+ 1

How to make a class in Java that contains a reference of type self ?

How can I make a class that contains a reference to self type. Pointers are not supported in Java. Reference types requires initialization during creation. The code is give below https://code.sololearn.com/c26HM9L9QwhG/?ref=app

24th Jun 2020, 5:18 AM
Terminal_Phantom
Terminal_Phantom - avatar
4 Respuestas
+ 2
Its best dont add self reference to object when using programming language that make use of garbace collection (like Java). You can access to self reference within class scope with this keyword public Class MyClass{ public void method(){ MyClass me= this; } }
24th Jun 2020, 5:51 AM
KrOW
KrOW - avatar
+ 2
public class MyClass { MyClass other; public MyClass(){ this.other= null; } public void assign(MyClass o){ this.other= o; } }
24th Jun 2020, 5:57 AM
KrOW
KrOW - avatar
+ 1
But, I want the object to contain reference to another object of type self.
24th Jun 2020, 5:53 AM
Terminal_Phantom
Terminal_Phantom - avatar
+ 1
Sorry. Previously my question was different.
24th Jun 2020, 5:55 AM
Terminal_Phantom
Terminal_Phantom - avatar