• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

init() and constructor combined?

 
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Ooops! Paul beat me to it!]
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/
 
Die Fledermaus does not fear such a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic