Win a copy of JDBC Workbook this week in the JDBC and Relational Databases forum
or A Day in Code in the A Day in Code 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Help! Jsp Declaration Question

 
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.
 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic