I dont understand , how to figure out the last statement.
Actually from my previous learning, I use to learn that , a reference is use to call methods of of an instance of a class.
So, I see "frame" as a reference and "frame" calls "getContentPane()" method,
where from this:
.add(button) comes in the same statement ??
Is it like, "frame" is calling two methods(i.e. getContentPane() and add(button) ) at the same time ???
Then check the API for getContentPane() to see what it returns; you will then understand why you can do that.
If you are familiar with any OO language, this shouldn't confuse you.
But now I am reading Head First Java, in which I came across this feature(method chaining).
So, Thank you so much Kemal Sokolovic for guiding me.
So, I see "frame" as a reference and "frame" calls "getContentPane()" method
If we go into such details ignoring original question:
See the extra brackets?
I must correct you - these are not brackets but parentheses .
Campbell Ritchie wrote:But the off‑topic discussions are always much more fun
Ohh come on Campbell ! , At least you should maintain the decorum , don't forget, you are leader of the forum, these childish things doesn't suit you as a sheriff !!
So, please come to the point, and make me clear about that method chaining thing !!
Matthew Brown wrote:
But it's been answered. Which bits are you still having trouble with?
Please explain me step by step the functioning of this statement i.e. how it works actually ?
What I understand is that :
frame first invokes the getContentPane() method, right ?
getContentPane() method is placed on the stack, right ?
getContentPane() method futher calls the add(button) method, right ?
add(button) method is placed above the getContenPane() method on the same stack, right ?
add(button) method finishes
add(button) method is popped off the stack
control comes returns to the getContentPane() mthod
getControlPane() method finishes and it is also popped off from the stack.
Now, I feel , I am, somewhere wrong in that when I said " getContentPane() calls the add(button) method"
Please make me clear on this, if you could do so !!
Rubbal Bhusri wrote:Please explain me step by step the functioning of this statement i.e. how it works actually ?
1. 'frame' is a JFrame. That means it has a getContentPane() method.
2. getContentPane() returns a Container, which has an add() method.
3. Container.add() takes a Component, and a JButton is a Component.
4. The dots just join all of it together. Providing the next method call is valid for the object returned by the previous method, you can chain things out as long as you like.
As for all that "placed on the stack"/"popped off the stack" stuff: FORGET IT.
It's not relevant to understanding the flow, and to be honest I really don't care exactly how it's done. And neither should you.
is almost identical to:
The only difference between the two is that in the latter case you still have the contentPane variable, which you can use again. So the method chaining is most useful when you don't need to keep the values of the intermediate steps.
So it's not true to say that the getContent() method calls the add(button) method. The getContent() method returns a value to this method, and then add(button) is called on that return value. The getContent() method fully completes before the add(button) method is called.
You can have chains like this as long as you want. Each step a method is called on the return value of the previous method call.
Does that make sense now?