-2

Argument of an method is final object. Why?

Does it means that in this method (service) this object (response) is like final class (You cannot override methods of that class /HttpServletResponse/ in this object /response/)?

Or it means that in this scope (inside this method /service/) You can't change reference of that argument object (response) to another, let say new, HttpServletResponse instance (in that scope)?

Like:

response = new HttpServletResponse();

Here is a code example:

public class ServletLifeCycleExample extends HttpServlet {

    private int count;
...
    @Override
    protected void service(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
        getServletContext().log("service() called");
        count++;
        response.getWriter().write("Incrementing the count to " + count);
    }
...
}
zelenooq
  • 137
  • 1
  • 1
  • 10

1 Answers1

2

Like you said final indicates that you can't change the reference to something else.

i.e.

response = new HttpServletResponse();

or

response = null;

are prohibited.

But you can change the attributes of response object

i.e. something like

response.setStatus(status)

would be allowed.

Shanu Gupta
  • 3,699
  • 2
  • 19
  • 29