Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

can we write a method inside the method in java?

 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there any possible to write method inside a method.why i am asking this is in the below code

Enumeration en = getServletConfig().getInitParameterNames();
thorugh calling the getServletConfig() method we can access the getInitParameterNames().Can anyone give some explanation please?
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if getServletConfig() returns a Class with a public method called getInitParameterNames() then sure.

Object/Class chaining can be a double-edged sword so be careful. It is quite easy to do via the Singleton patern, or factories,
but you will have to be careful with the instantiation or creation of too many temporary objects.

WP
 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not writing a method within a method, though. It's just calling a method on whatever was returned by the first method.

What you've got there is exactly equivalent to:
except that you don't get to keep a reference to the ServletConfig to use again later.
 
Sheriff
Posts: 67671
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

William P O'Sullivan wrote:Object/Class chaining can be a double-edged sword so be careful. It is quite easy to do via the Singleton patern, or factories,


This has nothing at all to do with singletons or factories. Any object that is returned from a method can be chained in this manner.
 
William P O'Sullivan
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not quite true, each Object in the chain must declare a public method corresponding to the next entry.

Each method could instantiate a brand new Object as well, so without full access to the API, quite dangerous.


getA().getB().getC().getD() etc..


WP
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No more danagerous than doing it longhand, though. The alternative is assigning to local variables, which creates just as many objects.
 
Bear Bibeault
Sheriff
Posts: 67671
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

William P O'Sullivan wrote:Not quite true, each Object in the chain must declare a public method corresponding to the next entry.


Each class must have the method, of course. What's your point? It still has nothing at all to do with singletons.

Each method could instantiate a brand new Object as well, so without full access to the API, quite dangerous.


Not at all. Regardless of how the methods are called, they will do the exact same thing. The methods have no knowledge of whether they were called as part of a chain or not.
 
William P O'Sullivan
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All I'm saying and I've seen this in production,

is that in my example, for instance the getB() from an A Class, reran a monster SQL which was then VOed, then returned and passed to C() for processing.

Problem was the connection was never closed!

WP
 
Bear Bibeault
Sheriff
Posts: 67671
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That would happen however the method was called. It has nothing at all to do with the subject of method chaining.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

William P O'Sullivan wrote:
Each method could instantiate a brand new Object as well, so without full access to the API, quite dangerous.


I agree. however it is quit dangerous always ... not only with method chaining
 
Ravi kapa
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:It's not writing a method within a method, though. It's just calling a method on whatever was returned by the first method.

What you've got there is exactly equivalent to:
except that you don't get to keep a reference to the ServletConfig to use again later.


Thanks for the reply i got it.....
reply
    Bookmark Topic Watch Topic
  • New Topic