aspose file tools*
The moose likes Servlets and the fly likes why init() method is not final? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "why init() method is not final?" Watch "why init() method is not final?" New topic
Author

why init() method is not final?

r suraaj
Greenhorn

Joined: Oct 26, 2008
Posts: 16
in the javax.servlet.GenericServlet; class why init (ServletConfig cg) is not finalized? i mean there is another init() to over ride, but why they doesn't made init (ServletConfig cg) as final method? we can easily override this method in our class., so what is the use if we override this version of init(-) rather than over riding init()?

thanks in advance.,
eagerly waiting for your replies.,
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

You can choose to override either.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
pankaj vijay
Ranch Hand

Joined: Apr 01, 2008
Posts: 75
when init() method is called there is no servlet instance is associate with this servlet class. init() is first method called to make this servlet class a real servlet. at this time there is no servletConfig object also. so you can use init(ServletConfig config) method to pass other servlets ServletConfig object. if it is final then you can not do this.


Pankaj Vijay (SCJP, SCBCD)
Learn Core Java,Learn Servlet Jsp, SCJP Questions,Struts Tutorial
sagar powar
Greenhorn

Joined: Jan 13, 2010
Posts: 8
Pankaj, I agreed that one can override any init() method in your Servlet class. But I didn't get your point:

"you can use init(ServletConfig config) method to pass other servlets ServletConfig object"

Can you please provide more details?


SCJP 1.4 - 93%
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41526
    
  53
pankaj vijay wrote:at this time there is no servletConfig object also. so you can use init(ServletConfig config) method to pass other servlets ServletConfig object.

I don't quite follow the logic behind this, but your code should neither call other servlet's init methods, nor pass around ServletConfig objects.


Ping & DNS - my free Android networking tools app
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

pankaj vijay wrote:you can use init(ServletConfig config) method to pass other servlets ServletConfig object.


that is what GenericServlet class do for you! . you no need to do this again. if you want to Override init then always use init()[Servlet version] .

GenericServlet's init(config)
C Shinde
Ranch Hand

Joined: Oct 22, 2009
Posts: 60
public void init(ServletConfig config)
throws ServletException

Called by the servlet container to indicate to a servlet that the servlet is being placed into service. See Servlet.init(javax.servlet.ServletConfig).

This implementation stores the ServletConfig object it receives from the servlet container for later use. When overriding this form of the method, call super.init(config).


This is what init(config) method is used, you can overwrite this method but then you will need to call the Super.init(config) method, because if you forget this then, I suppose the initialization process of your servlet will not complete properly, the servlet does not have access to the ServletConfig and the application throws a NullPointerException if you try to access it.

Instead there is another method


public void init()
throws ServletException

A convenience method which can be overridden so that there's no need to call super.init(config).

Instead of overriding init(ServletConfig), simply override this method and it will be called by GenericServlet.init(ServletConfig config). The ServletConfig object can still be retrieved via getServletConfig().


Making a method final would be necessary if there would be a huge impact if someone tried overriding the method, if you look at what code is there in the init(config) method
there is nothing that one will miss or do after overriding as there is nothing one can do in the init method with respect to different JSP API/JSP implicit objects. Nothing is available so I think they thought if it is not going to be misused why even make it final. Though it throws an NullPointer but then thats trivial, not big enough problem to make a method final.

So the best practice is to use init() method and not init(config) method and forget final for these methods.

Read about final on Wiki and there are some links on the same page that talk about why and when to use final.

Finally, have a nice day!


Regards,
Chetan Shinde
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why init() method is not final?