Change property attribute object of an final object

时间:2018-07-24 10:11:20

标签: java final

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);
    }
...
}

1 个答案:

答案 0 :(得分: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.