• Post Reply Bookmark Topic Watch Topic
  • New Topic

Will the value incremented or remains the same.

 
Shishir malviya
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi frnd's ,

If we declare a instance variable (not static) in the init() method and initialize it with 0. Now we increment this variable in the service() method and displaying it.
If suppose one thousand request, invoke the service method. Will every request gets the value incremented by previous request or gets 1 only.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't declare an instance variable of a class inside a method of that class. Your question makes no sense.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your phrasing needs a little work. Of course, you can't declare an instance variable in a method. But I think what you're saying is you should declare an instance variable, initialize to zero in the init method, and then see what happens when the Servlet runs.

Fine. Somethign will happen. If only one istance is created, the instance variable will be incremented each time the service method is called. It will almost look like a static variable.

Without being coy, I'd say "So what?" Essentially, what is your motivation for asking this question? Do you want to prove that only one instance is created when the servlet loads? Do you want to evaluate how a Servlet is threaded.

And remember, in a clustered environment, all this logic goes out the window. With multiple JVMs, you'll have multiple instances, and that variable won't be synchronized.

What is your motivation for asking the question? With that knowledge, maybe we can dig a little deeper.

-Cameron McKenzie
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!