Meaningless Drivel is fun!*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes init() method doubt? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "init() method doubt?" Watch "init() method doubt?" New topic
Author

init() method doubt?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Guys,

Why should we have a super.init(config) when we override the init method that takes ServlerConfig reference??


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2491
    
    8

In order to call the functionality of your superclass.

If you don't call super.init(), you replace your superclass functionality with your own functionality.
If you do call super.init(), you extend our superclass functionality.

Regards, Jan


OCUP UML fundamental and ITIL foundation
youtube channel
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9345
    
    2

Tanks Jan!
Rishi Chopra
Ranch Hand

Joined: Mar 07, 2004
Posts: 108
hi read it few days back from some where

GenericServlet Code snippet :--

private transient ServletConfig config;

public void init (ServletConfig config) throws ServletException {
this.config = config;
init();
}

public void init() throws ServletException {
}


Above code indicates that the init() method with no args is called from within the init(ServletConfig) method.
If we override the init(ServletConfig) method that takes a ServletConfig then super.init(config) should be the first statement inside that overridden method.

Because the container creates a ServletConfig instance and passes that instance to the init method of the GenericServlet where that config instance is assigned to the local private copy of that class. (private transient ServletConfig

GenericServlet Code Snippet (in addition to mentioned above)

public ServletConfig getServletConfig() {
return config;
}
If we override the init(ServletConfig) method that takes a ServletConfig and didn't specify the super.init(config) then the local copy of the ServletConfig instance will not be initialized and if you try to use the getInitParameter(String) method of the ServletConfig interface (which has not been initialized to some value in the GenericServlet class) a NullPointerException will be the result.


Regards
Rishi Chopra


Life is Beautiful Learn to See it.........
Sandeep Vaid
Ranch Hand

Joined: Feb 27, 2006
Posts: 390
[CODE] GenericServlet Code snippet

private transient ServletConfig config;

public void init (ServletConfig config) throws ServletException {
this.config = config;
init();
}

public void init() throws ServletException {
}

Rishi Can you please let me know where did you find this code snippet? As i am aware that init(ServletConfig) stores this config object for future use, i was wondering that in which instance variable it is storing this as GenericServlet class has no such "private transient ServletConfig config" instance variable (according to J2EE1.4 Java Docs) .
Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
this concept, has been explained in one line in the book and i believe it should be explained more properly.


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
 
jQuery in Action, 2nd edition
 
subject: init() method doubt?
 
Similar Threads
difference b/w init() and init( config)
init of servlets and jsp
Context
Call destroy() inside init()
Garbage Chatacters