File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Declarations" Watch "Declarations" New topic
Author

Declarations

Drew Lane
Ranch Hand

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

<%! int x = 44; %>
Is one a local variable and the other is not?
Thanks,
Drew
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;
}
and
<%! 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.
MS
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Declarations
 
Similar Threads
Mock Exam qs...
Declare variables in scriptlet?
why infinite loop
Variables Scope Question
output