top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Java: Why we can declare arraylist as final and what is impact on this?

+3 votes
2,903 views
Java: Why we can declare arraylist as final and what is impact on this?
posted Oct 6, 2013 by Nagarajuk

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+2 votes

When you make an arraylist as final you cannot rebind the variable to point to a different collection instance: for example -

final List<Integer> list = new ArrayList<Integer>();
list = new ArrayList<Integer>(); // Since `list' is final, this won't compile
answer Oct 6, 2013 by Deepankar Dubey
+1 vote

final as you know means that you cannot re-assign that object reference after it is assigned. But you can call any method of the object, so you can perform insert, search and any other operation to the object.

answer Oct 7, 2013 by Satyabrata Mahapatra
Similar Questions
+2 votes
public class ServletClassName extends HttpServlet {
//declare instance variables for the servlet
int  instanceVar ; // not thread−safe
// overriding the Generic Servlet.init ( )
@Override
public void init ( ) throws ServletException {
// initialize instance variables, etc.
instanceVar = 0 ;
}
. . .
}

Per each page request, in the service() method (which eventually will be either doGet(), doPost(), or whatever),
you can manipulate instance variables.

Write a Servlet that counts the total number of visits (how many times it's being visited from all clients). The Servlet should display the following information

• Total Page Visits:
• Client Remote Address:
• Client Host Address: <client_fully_qualied_url__or__client_IP_address>

The last two can be obtained from the HttpServletRequest, Refer to the Java EE API

then how can re-do these using the Java API for HttpSession.

...