You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Oliver Jonas <ol...@jonas.com> on 2004/12/02 16:00:28 UTC
JSP: final variable value lost in anonynous inner class
Hi,
I have the following test JSP (see below.) When I use the final variable
obtained by class A inside an anonymous subclass of B the values are lost.
However, if I define the final variables' values directly without using
class A it works. Both cases work fine when using this as a normal Java
class, so it seems related to JSP's or Tomcat.
The catalina.out output is:
1/ str1=str1
1/ str2=str2
1/ i1=1234
1/ i2=1234
2/ str1=null
2/ str2=str2
2/ i1=0
2/ i2=1234
a == null
This is Tomcat 4.1.30 with VM 1.4.2_06.
Do you know what is wrong or how to bypass this?
Thanks,
Oliver.
<%!
public static class A
{
private String s;
private int i;
public A() {
s = "str1";
i = 1234;
}
public String getS() {
return s;
}
public int getI() {
return i;
}
}
public A getA() {
return new A();
}
public static abstract class B
{
public B()
{
run();
}
public abstract void run();
}
public B getB()
{
final A a = getA();
final String str1 = a.getS();
final String str2 = "str2";
final int i1 = a.getI();
final int i2 = 1234;
System.out.println("1/ str1=" + str1);
System.out.println("1/ str2=" + str2);
System.out.println("1/ i1=" + i1);
System.out.println("1/ i2=" + i2);
return new B()
{
public void run()
{
System.out.println("2/
str1=" + str1);
System.out.println("2/
str2=" + str2);
System.out.println("2/ i1="
+ i1);
System.out.println("2/ i2="
+ i2);
if(a == null)
System.out.println("a == null");
}
};
}
%>
<%
getB();
%>