• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Life cycle query

 
Somesh Rathi
Ranch Hand
Posts: 31
Eclipse IDE Hibernate Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,
I have some silly but significant doubt related to Servlet Life cycle

What is the difference between Loading the servlet & instantiating the servlet ?
 
Ankit Garg
Sheriff
Posts: 9577
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think loading is the process where the server JVM loads the servlet class into memory. Instantiation happens after loading which may be any time before the first request for the servlet is received (if there is no load-on-startup associated with that servlet). I may be wrong here...
 
Abraham Moyo
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

loading and instantiation should be the same thing (I stand to be corrected)!

When a JVM create an instance of a java object it is called instantiation. And where else does it happen? In the memory. The class is made into a runtime object during instantiation and we have the result (the object) in the memory. So, as I see it class loading and instantistion is the same.

By the way, a servlet is a java class and so should follow suite.
 
Ankit Garg
Sheriff
Posts: 9577
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a class is loaded when the JVM starts or when you use a static member of the class or create an instance of the class. But a class is instantiated when you create an instance of the class which is always after the class is loaded...
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, as I see it class loading and instantistion is the same.

They're not. A class is loaded into memory only once (the first time an object of that class is about to be instantiated, or if a Class.forName() call for it is made). Instantiation, on the other hand, can occur many times - every time an object of that class is created.

As regards servlets, note that while most servlet containers will only create a single instance of a servlet and then use that for the lifetime of the web app, the container is free to take the servlet out of service, destroy the object instance, and then instantiate the class anew if it so pleases.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Abraham Moyo:

loading and instantiation should be the same thing (I stand to be corrected)!



There is a huge difference.
A static initializer executes at class load time, meaning it executes once
for each time the class is loaded (once per class loader).
A constructor executes each time an object is instantiated.
new TheObject();

Hope this help
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Ulf explained, it's class loading but object instantiation. At least that's how you can remember it, though some would argue instantiation is of the class, not the object (this depends on whether you view an object as an instance or as a modelled entity, which is modelled as a class). The latter is really shorthand for "instantiate a new object instance from a class definition". Nonetheless each term has a distinct meaning.
[ December 09, 2008: Message edited by: Charles Lyons ]
 
Somesh Rathi
Ranch Hand
Posts: 31
Eclipse IDE Hibernate Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot everyone!
I got it . Class loading means JVM will allocate some memory for the class. It will happen only once. Where instantiating will happen everytime object of that clas is created.

Thanks for your response !

Regards,
Somesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic