aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes declaration section variable is not getting reset for each refresh operation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "declaration section variable is not getting reset for each refresh operation?" Watch "declaration section variable is not getting reset for each refresh operation?" New topic
Author

declaration section variable is not getting reset for each refresh operation?

yamini nadella
Ranch Hand

Joined: Apr 13, 2004
Posts: 257
<HTML><BODY>
Hello 1
<B><%!
int i=10; // --- 1
public void b()
{
i = i +5;
}
public void a()
{
b();
i = i+10;
}
%></B>

Hello 2
<B><%
a();
out.print("first line "+i);
%></B>
</BODY></HTML>

output of above script is looking like below.

Hello 1 Hello 2 first line 25
Upto this it is looking fine. But when I clicked refresh button of Internet explorer then I got
Hello 1 Hello 2 first line 40
After pressing refresh button.
Hello 1 Hello 2 first line 55
After another refresh button.
Hello 1 Hello 2 first line 70

why I is acting like a static variable. is all variables declared in declaration tags (<%! %> acts like this.
How to make these variables to act dynamically? I mean whenever I click refresh then it has to show same values.

What is the scope of this variable is called ?
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
When you use the declaration directive, it creates declarations! Big surprise. So, these are actually instance variables that are created. That is why you are seeing the value continually increase. If you want a value that is "new" for each request, you will want to define your variable in a scriptlet.

Or, you can stop using scriptlets altogether and find a better way to do all this.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: declaration section variable is not getting reset for each refresh operation?