Home arrow static arrow Java Programming [Archive] - Overwritten method has no access to local variable if it contains field dat
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Overwritten method has no access to local variable if it contains field dat
This topic has 3 replies on 1 page.

Posts:22
Registered: 8/18/99
Overwritten method has no access to local variable if it contains field dat  
Jul 21, 2004 4:40 PM



 
Hi

Did you know that an overwritten method has no access to local variables containing a member field value when method is called from the constructor?

Example: Running this

    import java.awt.event.ActionEvent;    import javax.swing.AbstractAction;     public class TestBug {         private String datum = "Member-String";             public static void main(String[] args) {            new TestBug().new TestAction().actionPerformed(null);        }         private final class TestAction extends AbstractAction {             public void actionPerformed(final ActionEvent e) {                 final String localString1 = TestBug.this.datum;                final String localString2 = "Local-String"new Upper() {                     protected void print(String cb) {                         System.out.println(cb + ": " + localString1);                        System.out.println(cb + ": " + localString2);                    }                }.print("Method");            }        }    }     class Upper {         public Upper() {             print("Constructor");        }         protected void print(String calledBy) {             System.out.println("Upper-String");        }    }


Leads to

    Constructor: null    Constructor: Local-String    Method: Member-String    Method: Local-String


The localString1 is not accessible from the constructor while localString2 is. Do you know a workaround for it? Is it a bug?

Thanks
Daniel Frey
 

Posts:447
Registered: 3/8/01
Re: Overwritten method has no access to local variable if it contains field dat  
Jul 21, 2004 5:05 PM (reply 1 of 3)



 
Interesting, I ran this under Java 1.5 Beta 2 and got the right output:
Constructor: Member-StringConstructor: Local-StringMethod: Member-StringMethod: Local-String


One way to get past it would be to not use a constructor, rather, have something like an init() method that does the same:
    class Upper {        public Upper init() {            print("Constructor");            return this;       }        protected void print(String calledBy) {            System.out.println("Upper-String");        }    }    new Upper() { ... }.init().print("Method");


Another way, in some cases, would be to pass arguments to the constructor.
 

Posts:22
Registered: 8/18/99
Re: Overwritten method has no access to local variable if it contains field dat  
Jul 21, 2004 11:52 PM (reply 2 of 3)



 
Not to use the constructor works.

When passing arguments to the constructor, we run definitvely into the problem with "overwriding methods used in the constructor": Before able to initialize the given parameters, the method is called.

I filed a bug with Sun.

Daniel Frey
 

Posts:2,909
Registered: 13.8.2003
Re: Overwritten method has no access to local variable if it contains field dat  
Jul 22, 2004 2:14 AM (reply 3 of 3)



 
I think this exact "bug" was discussed a while ago and it's behaviour was different with different VMs. It's not about not being able to access it though, it's more about what gets initialized/done first.
 
This topic has 3 replies on 1 page.