• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

variable assignment problem

 
piero calca
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
piero calca
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understood !
thanks again
 
Campbell Ritchie
Sheriff
Pie
Posts: 50168
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic