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; %>
Nadine -SCJP, SCWCD, SCBCD
Carl Trusiak
Sheriff
Joined: Jun 13, 2000
Posts: 3340
posted
0
<%! %> 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 !
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; %>
SCJP2, SCWCD
Sohail Nasim
Greenhorn
Joined: Mar 28, 2002
Posts: 3
posted
0
How about this one <%! int i; %> <% i = 3; %> <% j = 8; %> <%! int j = 5; %> i is <%=i%> j is <%=j%> What is the output?
Goan Balchao
Ranch Hand
Joined: Mar 25, 2002
Posts: 93
posted
0
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.