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

init() and constructor combined?

Souvvik Basu
Ranch Hand

Joined: Apr 05, 2010
Posts: 96
I read 2 conflicting things in 2 different places. One says a constructor needs to run before the init() method can be run, because a constructor creates an object while the init() method gives the object servlet-specific capabilities. The other says that a constructor can replace the init() method, and everything that is done in the init() method can be done in the constructor itself.
I am confused as to which is correct. Can we really combine the constructor and init() method and make it into a constructor? thereby doing away with the init() method?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Here's the key thing -- a servlet is a Java class.

Now given that, which of those statements are true?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61207
    
  66

Souvvik Basu wrote:The other says that a constructor can replace the init() method, and everything that is done in the init() method can be done in the constructor itself.

If you read this at a website, remove its bookmark and be sure to never ever visit it again.
If you read it in a book, burn it, then soak the ashes in hydrochloric acid for 10 hours. Then put the ashes into a rocket and fire it into the sun.

It is false. In fact it so stupidly false as to be terrifying.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61207
    
  66

In fact, please post where you read this so that everyone else can know to avoid it like hot lava.
Souvvik Basu
Ranch Hand

Joined: Apr 05, 2010
Posts: 96
A servlet being a java object, it needs to be created for sure. And hence the constructor has to run. But can nothing else be put inside it? Things that we can and do put in the init() method?
Souvvik Basu
Ranch Hand

Joined: Apr 05, 2010
Posts: 96
lolzzz.....
thanks for the reply. I was browsing through sites at random and looking at their questionairre, when I came across this. Have closed the site since, so cant recall the name.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

You can put code in the constructor, sure. But there really shouldn't be anything worth putting in a constructor. And if you do create a constructor for your servlet, you're just going to confuse the next person who has to maintain it, if (as appears likely) they don't have a good grasp of the basic concepts.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61207
    
  66

[Ooops! Paul beat me to it!]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61207
    
  66

The only really useful things you can do when initializing a servlet involve interacting with the ServletConfig and ServletContext instances. This must be done in init(), not the constructor.

Not paying attention to the lifecycle of a Servlet is a guaranteed way to make sure things go ka-boom in a spectacular way/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: init() and constructor combined?