Reputation: 175
I have come across such a code and my Java knowledge is not enough for it - I am pretty sure it is something simple, but I have not found an explanation as don't know how to express it in google. Here is the abstracted code, I hope nothing is missing:
public class A{
Car car;
.
.
.
public A do() {
car.move(somewhere);
return this;
}
}
public class B{
protected A doSomething(final A a ){
a.do();
return a;
}
}
My first question is what does "return this;" mean here? http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html does not include such a case.
Second is how a.do() works in method doSomething()? Method do() is supposed to return a value, yet it is not assigned anywhere?
Lastly, I suppose that "a" returned from doSomething() was changed in this method. Is this allowed, as "a" is final?
Upvotes: 4
Views: 130
Reputation: 65793
Using return this;
at the end of methods is a technique called Method Chaining and can be a very convenient way of performing a chain of operations on an object.
StringBuilder
is a good example:
String s = new StringBuilder().append("Hello").insert(0, "Goodbye").delete(7, 12).append(" :)").toString();
NB: It can also be used to make code seem over-complex so it should only be used where appropriate.
Upvotes: 0
Reputation: 2840
what does "return this;" mean here?
As mentioned over and over this it is always used within a context of an object and it is a reference to that object.
public A do() {
car.move(somewhere);
return this;
}
Return this, in the do()
method just returns a reference to the current object.
Second is how a.do() works in method doSomething()? Method do() is supposed to return a value, yet it is not assigned anywhere?
Method do() just does some operation on the instance it is called from. because it is public it can be called from any place, and because it is not static it can be called only from an already created instance. That is why it needs to be called like a.do()
and thats what it returns: the a reference.You dont have to assign it to anything, it will still operate within a reference and its operation will eventually update the a related state.
Lastly, I suppose that "a" returned from doSomething() was changed in this method. Is this allowed, as "a" is final?
Perfectly allowed. Java does not support pass by reference method so you can change the object the a reference points to however you cannot change the reference itself withing doSomething()
method context.
Upvotes: 0
Reputation: 75
do()
is a method of class A
. It can be called on any instance (object) of class A
.
this
refers to the current instance.
return this
means that the method will return the instance of class A
on which it (the method) is applied.
I'm not sure what such a method can be used for, though.
Upvotes: 0
Reputation: 109547
(First do
is a keyword. So you renamed for clarity.)
return this;
allows:
protected A doSomething(final A a ){
return a.do();
}
Which is exactly the same.
final
variable declarations mean that you may assign only once to it (for fields), most often immediately at the declaration. So it is used for (dynamic) constants initialised only once.
Upvotes: 0
Reputation: 4609
a is final that means its memory address will not be changed again and a.do is simply calling the method do that will cause the move method of car to be called and return this returns the reference to the same object.
Upvotes: 0
Reputation: 3720
return this
Returns the object itself. In case of a.do()
a is returned. What is this good for you might ask? It enables this:
a.do().do().do();
Upvotes: 5
Reputation: 1129
return this;
- Being that, the return type is an A
object, when the .do()
method is called, the method will return this
, this
is the exact same instance that called it.
public A doSomething(final A a)
, the only contract here is that an A
object is returned. What happens in the code block doesn't matter, as long as a null
or class that extends A
is returned.
final A a
declaration, simply means the memory location address by variable a cannot be re-assigned.. i.e. a = null;
or a = new A()
would throw an exception
Upvotes: 1
Reputation: 3302
return this;
is exactly the same as any other return statement - it returns a reference to this
. For example, if I had a variable Car a
, the statement a.do()
would return a reference to the same car a
.
As for final
, it doesn't mean immutability - it just means you can't reassign other data to the memory location. You can modify the Car
that's already there - but not replace it with another Car
.
Upvotes: 0