This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes difference between static n non-static? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "difference between static n non-static?" Watch "difference between static n non-static?" New topic
Author

difference between static n non-static?

reehan ishaque
Ranch Hand

Joined: Mar 25, 2001
Posts: 54
hi folks!
wats the difference between declaring a class member variable static n non-static. only one instance is created for each request so there shouldnt be any difference between them. hope u guys will reply.
bye
Eric Howell
Ranch Hand

Joined: Nov 26, 2000
Posts: 63
Originally posted by reehan ishaque:
hi folks!
wats the difference between declaring a class member variable static n non-static. only one instance is created for each request so there shouldnt be any difference between them. hope u guys will reply.
bye

There is only one static variable per a class which always run before main(). For example, each time you create a new instance
there is still only one static variable.
You could use it as a counter that increments each time you create an instance. Each new instance of the class could have a greater value of the static variable.
Pranit Saha
Ranch Hand

Joined: Sep 09, 2001
Posts: 130
Whenevr u r declaring a class as static, it means that u need to make objects to access the data members of that class.. isn't it.. and whenever it's non-static u've to make the objects usually..
Pranit..
reehan ishaque
Ranch Hand

Joined: Mar 25, 2001
Posts: 54
i think im not being able to tell wat i really mean is. take a look of the following code n tell me rather i should use static variable or non-static. both the variable generating the same output.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SimpleServlet extends HttpServlet
{
static int i=0;
int j=0;
public void doGet (
HttpServletRequestrequest,
HttpServletResponseresponse
) throws ServletException, IOException
{
PrintWriterout;

response.setContentType("text/html");
out = response.getWriter();

out.println("<HTML><HEAD><TITLE>");
out.println("</TITLE></HEAD><BODY>");
out.println(i++);
out.println(j++);
out.println("</BODY></HTML>");
out.close();
}
}
PC RE
Greenhorn

Joined: Aug 28, 2001
Posts: 29
Remember servlet class will not get initialized every time u call a servlet. Only the service() method will be called which will inturn call either doGet() or doPost() methods.
so, static 'i' and non-static 'j' will give u the same results. If u want to reset the variables every time the servlet is called, try initializing them in the doGet() or doPost() methods.
for example....
public void doGet() {
j = 0;
}
roshan ingole
Greenhorn

Joined: Aug 18, 2001
Posts: 12
Just tell me either you are giving a request to that servlet from only one browser, because look static variable is same for all instances and other variable is different for different for different instances, but if you are requesting from a same browser then in your case you are incrementing both variables static and non-static simultaneously so it shows you a same result, try to give a request from more than one browsers and then see the result.
I am shure it will be different....
-Roshan
reehan ishaque
Ranch Hand

Joined: Mar 25, 2001
Posts: 54
hi roshan!
the output is same regardless of how many browsers i open. in other words both variables r showing persistent behavior.
roshan ingole
Greenhorn

Joined: Aug 18, 2001
Posts: 12
Hi Reehan,
PC saying the right thing, servlet get initializes only once so after once initialization initially both values i.e. static and nonstatic are 0, and when there is a request for the same servlet then again that is not initialized and they both are continued from last value. So for the request from another browser it tooks the last value present in those variable instade of initializing it, because they already initializes.
If you want that for perticular session that value will be incremented then either you have to use cookies or sessiontracking.
-Roshan
reehan ishaque
Ranch Hand

Joined: Mar 25, 2001
Posts: 54
so should i conclude that static variables r of no use when declared in servlets?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I think you might be confusing access with scope.

Static variables are common across instances of a class, so the keyword static has to do with access. So as an example, you'd use static variables to count the number of instances of a class. In that classe's constructor, you'd increment the static variable. And in the classes's getInstanceCount() method, you'd return the static variable.

But this is a servlet class. There is typically one instance of a servlet that is created by a servlet container, or perhaps more than one, but we shouldn't really care about that.

If on the other hand, you want a variable to retain it's value over multiple calls to a method (for example, doGet()), I don't know of any way to do this, other than declaring it as a class variable (outside of the method). This is a scoping issue then. So because *both* i and j are declared outside your doGet, they will of course, both contain the same value, regardless of whether one of them is declared static or not. The only thing that static provides in this case, is that all instances of the servlet class can access i, whereas j belongs only to a single instance of the servlet class.

The both show 'persistence' because they are scoped outside of the method in which you increment them and output them.

If you declared j inside of doGet(), the persistence would end.
[This message has been edited by Mike Curwen (edited September 14, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference between static n non-static?
 
Similar Threads
Difference between static and non-static members.
static object refrences and non static object refrences
help me plz
static and non static
JButton