+ 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
4 Answers
+ 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;
}
}
+ 2
public class MyClass
{
MyClass other;
public MyClass(){
this.other= null;
}
public void assign(MyClass o){
this.other= o;
}
}
+ 1
But, I want the object to contain reference to another object of type self.
+ 1
Sorry.
Previously my question was different.