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
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.
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);
}
}
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.
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;
}