aspose file tools*
The moose likes Beginning Java and the fly likes Two dot operators in single statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Two dot operators in single statement" Watch "Two dot operators in single statement" New topic
Author

Two dot operators in single statement

Rubbal Bhusri
Ranch Hand

Joined: Dec 30, 2010
Posts: 71
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

Joined: Jun 19, 2010
Posts: 825
    
    5

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.


The quieter you are, the more you are able to hear.
Rubbal Bhusri
Ranch Hand

Joined: Dec 30, 2010
Posts: 71
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

Joined: Oct 13, 2005
Posts: 38412
    
  23
If it is supposed to be method chaining, there should be more () than you wrote.
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
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


Programming is about thinking, NOT coding
Rubbal Bhusri
Ranch Hand

Joined: Dec 30, 2010
Posts: 71
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

Joined: Apr 06, 2010
Posts: 4367
    
    8

What Campbell means is:
won't compile. It should be

See the extra brackets?
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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

Joined: Apr 06, 2010
Posts: 4367
    
    8

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

Joined: Oct 13, 2005
Posts: 38412
    
  23
When speaking to people who are not native English (‍) British speakers I tend to say round brackets.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

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

Joined: Oct 13, 2005
Posts: 38412
    
  23
But the off‑topic discussions are always much more fun
Rubbal Bhusri
Ranch Hand

Joined: Dec 30, 2010
Posts: 71
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

Joined: Apr 06, 2010
Posts: 4367
    
    8

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

Joined: Dec 30, 2010
Posts: 71
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

Joined: Mar 17, 2011
Posts: 7677
    
  19

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4367
    
    8

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?
 
 
subject: Two dot operators in single statement