3 Replies Latest reply: Aug 2, 2012 7:07 AM by ramp RSS

    local vs static variable in JSP

    800839
      Hi,

      The below is a local variable :-
      <%
      int foo = 0;
      %>

      which is equivalent to initializing the variable with in the jspInit().
      --------
      the below become the class member variable
      <%!
      int foo = 0;
      %>

      Which is equivalent to declaring outside the jspInit()? Please clarify.

      Thanks.
        • 1. Re: local vs static variable in JSP
          r035198x
          797836 wrote:

          Please clarify.

          Thanks.
          What do you need to be clarified exactly? Do you know the difference between static and non static?
          • 2. Re: local vs static variable in JSP
            800839
            Static variable are class variables and non static are instance variables. Since java doesnot support global variables
            using the below:-

            <%!

            int i = 10;

            %>

            can be used by all the methods defined in the JSP page? Please correct me if i am wrong.
            • 3. Re: local vs static variable in JSP
              ramp
              797836 wrote:
              Hi,

              The below is a local variable :-
              <%
              int foo = 0;
              %>

              which is equivalent to initializing the variable with in the jspInit().
              No. Inside the _jspService()
              --------
              the below become the class member variable
              <%!
              int foo = 0;
              %>

              Which is equivalent to declaring outside the jspInit()? Please clarify.

              Thanks.
              Yes. It becomes an instance variable (bad idea)

              It takes lesser time to check this out in your generated servlet than post a question here and what's more - you would pick up a few more things on the way.