Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

declarations and scriptlets

 
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 );
%>
 
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
 
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
 
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
 
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).]
 
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.
 
Are you here to take over the surface world? Because this tiny ad will stop you!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic