Results 1 to 6 of 6

Thread: Servlet static variable

  1. #1
    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.

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    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

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    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.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

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

  5. #5
    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.

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

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

Similar Threads

  1. Static variable initialization
    By Dilbert in forum Software Development
    Replies: 3
    Last Post: 31-12-2009, 01:38 PM
  2. Replies: 2
    Last Post: 28-08-2009, 07:51 PM
  3. Is it possible to have static variable in python?
    By gvBlake in forum Software Development
    Replies: 2
    Last Post: 21-07-2009, 09:56 PM
  4. Replies: 3
    Last Post: 25-04-2009, 11:34 AM
  5. Can we use static variable in other programming languages
    By FlayoFish in forum Software Development
    Replies: 2
    Last Post: 25-04-2009, 10:56 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,267,610.86788 seconds with 17 queries