Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

init() and constructor combined?

 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20750
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, please post where you read this so that everyone else can know to avoid it like hot lava.
 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20750
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Ooops! Paul beat me to it!]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64623
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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/
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic