This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets 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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declarations
 
Similar Threads
why infinite loop
Declare variables in scriptlet?
Variables Scope Question
Mock Exam qs...
output