This discussion is archived
3 Replies Latest reply: Aug 2, 2012 5:07 AM by ramp RSS

local vs static variable in JSP

800839 Newbie
Currently Being Moderated
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 Pro
    Currently Being Moderated
    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 Newbie
    Currently Being Moderated
    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 Explorer
    Currently Being Moderated
    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.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points