• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method within a Method?  RSS feed

 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working between applets and GUI, and came across this smippet of code.

setLayout(new BorderLayout());

That looks like a method within a method to me, is that what it is, or is it something more to it?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new BorderLayout() invokes a constructor and returns an object reference. The method setLayout() wants just such an object reference as an argument.

Any argument can be an expression that evaluates to the proper type. This time they made a new thing of the proper type. You could call a method on the same object or another object that returns the type:

The options are endless. Neat, huh?
[ October 06, 2006: Message edited by: Stan James ]
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new BorderLayout() returns an object. that object is being passed as the argument to setLayout().

note that you could have done it this way...



Note that often it is perferable to split them into two lines... it can be hard to find problems if the inner method call fails and you end up passing a null object into your method. very strange things can happen
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!