File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Declarations Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Declarations" Watch "Declarations" New topic


Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
What's the difference between
<% int x = 44; %>

<%! int x = 44; %>
Is one a local variable and the other is not?
g madhava
Ranch Hand

Joined: Sep 14, 2001
Posts: 85
<% int x = 44; %>
==> This goes in to the _jspService() method
something like this,[which is effectively a local variable]
public void _jspService()
int x = 44;
<%! int x = 44; %>
goes into the instance declaration. So this is a class level variable.
Mark Stein
Ranch Hand

Joined: May 20, 2002
Posts: 75
Yes, technically one is a local varible, and one is not, but you need to know more.
Declarations (<%! int x = 44; %> are initialized only once, when the page is first loaded. They are outside the jsp service method, so you can also declare a method using the <%! %> tag.
<% int x = 44; %> is a scriptlet. What you've done is declare a local variable that will be called in the jsp service method each time the page is called.
I suggest you read up on the different jsp tags, Declarations, Scriptlets, and Page Directives, and also the jsp life cycle. It'll help you understand things a little better, plus both are important for the test.
I agree. Here's the link:
subject: Declarations
It's not a secret anymore!