Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init() method doubt?

 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

Why should we have a super.init(config) when we override the init method that takes ServlerConfig reference??
 
Jan Cumps
Bartender
Posts: 2599
12
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joe Harry
Ranch Hand
Posts: 10124
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tanks Jan!
 
Rishi Chopra
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sandeep Vaid
Ranch Hand
Posts: 392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 1277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this concept, has been explained in one line in the book and i believe it should be explained more properly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic