• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

declarations and scriptlets

 
Zhining Zhang
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
Could anyone explain the differents among the following 3 cases?
scope ???
thanks! -Zhining Zhang
case 1: ====================
<%!
String str = "zhining";
%>
<%
out.println( "str=" + str );
%>
case 2: ==========
<%
String str = "zhining";
out.println( "str=" + str );
%>
case 3: ===========
<%!
String str = "zhining";
%>
<%
String str = "zhining2";
out.println( "str=" + str );
%>
 
Win Yu
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
case1 is the same with case2. Case3 may get compile error when run. Correct me if I am wrong. By the way, where you got this kind of question. I am searching for more quiz to practise.
[This message has been edited by Win Yu (edited November 08, 2001).]
[This message has been edited by Win Yu (edited November 08, 2001).]
 
Zhining Zhang
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

case 3 is correct too. the output is
str=zhining2
 
Max Tomlinson
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes- it comes down to scope.
In Case 1, str is a declaration, which makes it an instance variable
In case 2 , str is a local (method) variable (inside _jspService)
In case 3, the first str is an instance variable, but the second one is a local variable, which is the one called in the out statement.
max
 
Max Tomlinson
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes- it comes down to scope.
In Case 1, str is a declaration, which makes it an instance variable
In case 2 , str is a local (method) variable (inside _jspService)
In case 3, the first str is an instance variable, but the second one is a local variable, which is the one called in the out statement.
max
 
Sreenivasa Majji
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got a compilation error for case 3 (I'm using Resin 2.0.3 with JDK1.3.1).
The error description:
----------------------------------------------------------------
/ScopeChecking.jsp:22: Duplicate variable declaration: String str was String
str
String str = "zhining";
^
1 error
----------------------------------------------------------------
I can provide the generated .java file (if you want to).

Originally posted by Max Tomlinson:
yes- it comes down to scope.
In Case 1, str is a declaration, which makes it an instance variable
In case 2 , str is a local (method) variable (inside _jspService)
In case 3, the first str is an instance variable, but the second one is a local variable, which is the one called in the out statement.
max


------------------
Sreenivasa Kumar Majji
Sun Certified Java Programmer
SCJP Mock Test
 
deepak adlakha
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
all three are working in tomcat.It's just a case of the scope of the varirable.
when we are defining again in scriplet that means,local variable has taken over predence over global variable.
That's it.

[This message has been edited by deepak adlakha (edited November 08, 2001).]
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The real difference would be seen when the java file was generated
Case1

Case2

Case3

------------------
I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
[This message has been edited by Carl Trusiak (edited November 09, 2001).]
[This message has been edited by Carl Trusiak (edited November 09, 2001).]
 
Win Yu
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,Carl. Very clear.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic