aspose file tools*
The moose likes Beginning Java and the fly likes can we write a method inside the method in java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can we write a method inside the method in java?" Watch "can we write a method inside the method in java?" New topic
Author

can we write a method inside the method in java?

Ravi kapa
Ranch Hand

Joined: Jan 05, 2012
Posts: 87
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?
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

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
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4421
    
    8

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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

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

Joined: Apr 06, 2010
Posts: 4421
    
    8

No more danagerous than doing it longhand, though. The alternative is assigning to local variables, which creates just as many objects.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

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

Joined: Mar 28, 2012
Posts: 859

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61419
    
  67

That would happen however the method was called. It has nothing at all to do with the subject of method chaining.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Jan 05, 2012
Posts: 87
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.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can we write a method inside the method in java?