Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Servlet static variable

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 06-03-2010
Member
 
Join Date: Nov 2009
Posts: 877
Servlet static variable
  

Hello, I am learning Java programming and currently working on the Servlet. I have learn to create a static variable in Servlet. But, I don't know why but it is providing me error while declaring it. I am also not able to understand the use of the static variable in servlet. If you are having any details about it, then please provide that to me. Thank you in advance.

Reply With Quote
  #2  
Old 06-03-2010
Member
 
Join Date: Oct 2005
Posts: 2,389
Re: Servlet static variable

Hello, If you are making use of the Servlet then then you can use different places in it for storing the data:
  • Local variables
  • Request attributes
  • Session attributes
  • Instance variables
  • Static variables
  • Context attributes
Reply With Quote
  #3  
Old 06-03-2010
Member
 
Join Date: May 2008
Posts: 2,384
Re: Servlet static variable

As the static variables are shared in all of the instance of the class. And if you are making use of the non static variable then that will be specifically used for the instance of that particular class. If you are making use of the Servlet then according to the servlet specification if your servlet is not declaring the SingleThreadModel then it will have one and only one instance.
Reply With Quote
  #4  
Old 06-03-2010
Member
 
Join Date: Feb 2008
Posts: 1,848
Re: Servlet static variable

Hello, I have the code below which will be helpful to know the details about the
Code:
static variable in java:
public class Server extends HttpServlet
{
public static int count = 0;
public void doGet(HttpServletRequest request, HttpServletResponse response)
{
count++;
response.getWriter().println(count);
}
}
Reply With Quote
  #5  
Old 06-03-2010
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: Servlet static variable

When a number of objects are created from the same class blueprint, they each have their own distinct copies of instance variables. In the case of the Bicycle class, the instance variables are cadence, gear, and speed. Each Bicycle object has its own values for these variables, stored in different memory locations. Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.
Reply With Quote
  #6  
Old 06-03-2010
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: Servlet static variable

Java provide the facility of the static variables in place of the global variables in other languages.
Code:
public class Test 
 {
   static int first;
  }
If you declare such variable then it will simply assigned with the same memory location. And it is possible to use such variable even when not a single instance is called. Static variables are also known as Class variables. But, if you are declaring that class property as a final then it will be termed as global constant.
Code:
  public class Test
 {
    final static first;
 }
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Servlet static variable"
Thread Thread Starter Forum Replies Last Post
Static variable initialization Dilbert Software Development 3 31-12-2009 01:38 PM
Runtime Error 91 : Object Variable or with block variable not set Ryan21 Software Development 2 28-08-2009 08:51 PM
Is it possible to have static variable in python? gvBlake Software Development 2 21-07-2009 10:56 PM
What is Global Variable static? Declaring static variable instead of Global variable Reckon Software Development 3 25-04-2009 12:34 PM
Can we use static variable in other programming languages FlayoFish Software Development 2 25-04-2009 11:56 AM


All times are GMT +5.5. The time now is 09:57 PM.