Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Help! Jsp Declaration Question  RSS feed

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please tell me why this code causes a compilation error?
<html>
<body>
<%! int i; %>
<%! i = 12; %>
Value of i is: <%= ++i %>
</body>
</html>
Isn't
<%! int i; %>
<%! i = 12; %>
equivalent to
<%! int i = 12; %>
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<%! %> is the declaration inside this you can place methods or anything you can place OUTSIDE a method in a java class. The following is illegal in Java

This is exactly the type of thing that you are trying to do. You can make this right by removing the !
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
<%! int i; %>
<%! i = 12; %>
is not equivalent to
<%! int i = 12; %>
why?
<%! i = 12; %> doesn't satisfy with the defination of a declaration because there's not an identifier for var i. what you intended to do is more like a scriptlet <% i = 12; %>
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about this one
<%! int i; %>
<% i = 3; %>
<% j = 8; %>
<%! int j = 5; %>
i is <%=i%>
j is <%=j%>
What is the output?
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That should be OK. This is because the declarations are assimilated and declared as instance variables in the page implementation class.
So j is available to the code before it.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!