Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Multiple Method Calls

  Asked By: Aysel    Date: Jun 27    Category: Java    Views: 2297
  

I am trying to find out the purpose of calling more than one method
at the same time and storing the return value in a variable. For
instance:

String myRequestFile= request.getServletPath();
String myFile=getServletConfig().getServletContext().getRealPath
(myRequestFile);

Any explainations or references were I can read up on this will be
greatly appreciated.

Share: 

 

1 Answer Found

 
Answer #1    Answered By: Abana Cohen     Answered On: Jun 27

The long line of code:
String
myFile=getServletConfig().getServletContext().getRealPath(myRequestFile);

is the same as:

ServletConfig servletConfig = getServletConfig();
ServletContext servletContext = servletConfig.getServletContext();
String myFile = servletContext .getRealPath(myRequestFile);

Its just all chained together.

The object first in the chain, pulls on the next in line which pulls the
next in line without having to explicitly giving a name for each step of
the way.

You can chain the methods and shorten your code.

After a point its gets less readable, from the programmer's point of view.

It all could also be shortened to:

String
pathToMyFile=getServletConfig().getServletContext().getRealPath(request.getServl\
etPath());

I was looking at the documentation and it appeared that
getServletConfig().getServletContext().
was redundant.

 
Didn't find what you were looking for? Find more on Multiple Method Calls Or get search suggestion and latest updates.




Tagged: