Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can we write a method inside the method in java?

 
Ravi kapa
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • 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?
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • 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: 94
  • Mark post as helpful
  • send pies
  • 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.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic