tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Allistair Crossley" <Allistair.Cross...@QAS.com>
Subject RE: final variable value lost in anonynous inner class
Date Thu, 02 Dec 2004 15:03:44 GMT
this looks like a school assignment ;)

> -----Original Message-----
> From: Oliver Jonas [mailto:oliver@jonas.com]
> Sent: 02 December 2004 15:00
> To: tomcat-user@jakarta.apache.org
> Subject: 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();
> 
> %>
> 
>  
> 
> 


<FONT SIZE=1 FACE="VERDANA,ARIAL" COLOR=BLUE> 
-------------------------------------------------------
QAS Ltd.
Developers of QuickAddress Software
<a href="http://www.qas.com">www.qas.com</a>
Registered in England: No 2582055
Registered in Australia: No 082 851 474
-------------------------------------------------------
</FONT>


---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org


Mime
View raw message