File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes variable assignment problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "variable assignment problem" Watch "variable assignment problem" New topic

variable assignment problem

piero calca

Joined: Jan 05, 2012
Posts: 4
In the book: Introduction to java programming ( good , tons of exercises) i found this code (referred to earlier version of Java SDK) :

This sounds OK , we have a type (Container) a name and an assignment : the method getContentPane() returns the needed object.
But in the book HF Java ( great in explaing though concepts ) i read :

and i'm wondering : were is landing the object , since there is no type and no name ?
Thanks a lot for any explanation
Darryl Burke

Joined: May 03, 2008
Posts: 5115

It's called method chaining. In your sample code, you are interested in adding button to the JFrame's contentPane, which, being a Container, has a method for that.

Perhaps a pair of redundant parentheses will make it more clear to you:Which means, take the Container returned by frame.getContentPane() and invoke its add(...) method, passing button as a parameter.

edit And, for your future reference, please UseCodeTags <-- link

luck, db
There are no new questions, but there may be new answers.
piero calca

Joined: Jan 05, 2012
Posts: 4
Understood !
thanks again
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46320
By the way: you don’t need myFrame.getContentPane().add(myButton); unless you use an old version of Java eg JDK1.4.
You can write myFrame.add(myButton);
Read this to find out why.
I agree. Here's the link:
subject: variable assignment problem
jQuery in Action, 3rd edition