Can a Java object pass itself an instance of itself
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.method开发者_开发知识库ToCall(itsObject);
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).
Yes, you can.
One (contrived) example:
BigInteger one = BigInteger.ONE;
BigInteger two = one.add(one);
(You should try these things in your IDE - it takes less time than writing a question)
Yes. There is nothing that prevents you doing this.
It would be by default,
you don't need to specify it. in method body you can refer it as this
Note: method should not be static
and if you want to externally specify you can do it simple.
Yes, you can do this, as long as methodToCall accepts an object of type SomeClass(or a class deriving from SomeClass) as a parameter:
public void methodToCall(SomeClass parameter){.....}
You can call it from outside your class:
yourObject.methodToCall(yourObject)
or from within the class, using this :
public class SomeClass{
...
public void AnotherMethod(SomeClass parameter)
{
this.methodToCall(this);
}
...
}
精彩评论