D
Daniel
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
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