Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a Java object pass itself an instance of itself

Tags:

java

I have a class called SomeClass which has a method called methodToCall(SomeClass o)

If I instantiate SomeClass like this:

SomeClass itsObject = new SomeClass();

Can I then do this:

itsObject.methodToCall(itsObject);
like image 306
Ankur Avatar asked Nov 29 '22 04:11

Ankur


1 Answers

Absolutely. How that will behave will depend on the implementation, of course.

Just as an example, equals is defined to be reflexive, such that:

x.equals(x)

should always return true (assuming x is non-null).

like image 153
Jon Skeet Avatar answered Dec 09 '22 13:12

Jon Skeet