Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two dot operators in single statement

 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain the following statement :

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,
Then
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 ???
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a similar topic here that you might find useful: method chaining.
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.
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am a beginner, and the text-books which I followed, were so basic and simple that they never tend to use these short-cuts like method-chaining etc.
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is supposed to be method chaining, there should be more () than you wrote.
 
Vishal Shaw
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rubbal Bhusri wrote: Actually from my previous learning, I use to learn that , a reference is use to call methods of of an instance of a class.


Just one correction, "a reference is use to call members of a class".

Hope now it makes sense
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If it is supposed to be method chaining, there should be more () than you wrote.

Now that , you put a question on the other guys post , I would like you to please clear all the doubts.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What Campbell means is:
won't compile. It should be

See the extra brackets?
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was certainly a typo, code was probably not pasted, since in the other part of the post Rubbal Bhusri wrote:
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 .
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's just a localization problem . In the UK we'd often refer to those as brackets (then [] are square brackets). But you're right, on this site it would probably be less likely to lead to confusion to say parentheses.
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When speaking to people who are not native English (‍) British speakers I tend to say round brackets.
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I recall, this isn't a topic about British or any other vocabulary, but I can see my point is taken - paying attention to (in this case) irrelevant details can easy lead to off-topic discussion. ;)
Cheers!
 
Campbell Ritchie
Sheriff
Pie
Posts: 50241
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But the off‑topic discussions are always much more fun
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rubbal Bhusri wrote:So, please come to the point, and make me clear about that method chaining thing !!

But it's been answered. Which bits are you still having trouble with?
 
Rubbal Bhusri
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
then
getContentPane() method is placed on the stack, right ?
then
getContentPane() method futher calls the add(button) method, right ?
then
add(button) method is placed above the getContenPane() method on the same stack, right ?
then
add(button) method finishes
and then
add(button) method is popped off the stack
and then
control comes returns to the getContentPane() mthod
then
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 !!
 
Winston Gutkowski
Bartender
Pie
Posts: 10527
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Winston
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I think this is the easiest way to think about it. This line here:
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic