Java Nested Classes
You can declare fields and methods inside an anonymous class, but you cannot declare a constructor. You can declare a static initializer for the anonymous class instead, though. Here is an example:
final Strint textToPrint = "Text..."; MyInterface instance = new MyInterface() { private String text; //static initializer { this.text = textToPrint; } public void doIt() { System.out.println(this.text); } }; instance.doIt();The same shadowing rules apply to anonymous classes as to inner classes.
@reference_1_stackoverflow
Accessing constructor of an anonymous class
Answer 1:
From the Java Language Specification, section 15.9.5.1:
An anonymous class cannot have an explicitly declared constructor.As an alternative, you can create some final local variables, and/or include an instance initializer in the anonymous class.
public class Test {
public static void main(String[] args) throws Exception {
final int fakeConstructorArg = 10;
Object a = new Object() {
{
System.out.println("arg = " + fakeConstructorArg);
}
};
}
}
Answer 2: ......
You can add an anonymous initializer like this:final int anInt = ...;
Object a = new Class1()
{
{
System.out.println(anInt);
}
void someNewMethod() {
}
};
Don't forget final on declarations of local variables or parameters used by the anonymous class, as i did it for anInt.Answer 3:
Here's another way around the problem:
public class Test{
public static final void main(String...args){
Thread t = new Thread(){
private String message = null;
Thread initialise(String message){
this.message = message;
return this;
}
public void run(){
System.out.println(message);
}
}.initialise(args[0]).start();
}
}
@reference_2_stackoverflow
How to pass parameters to anonymous class?
Answer 1:
Technically, no, because anonymous classes can't have constructors.
However, classes can reference variables from containing scopes. For an anonymous class these can be instance variables from the containing class(es) or local variables that are marked final.
edit: As Peter pointed out, you can also pass parameters to the constructor of the superclass of the anonymous class.
However, classes can reference variables from containing scopes. For an anonymous class these can be instance variables from the containing class(es) or local variables that are marked final.
edit: As Peter pointed out, you can also pass parameters to the constructor of the superclass of the anonymous class.
– Peter Lawrey
Feb 24 '11 at 17:08
Answer 2:
Yes, by adding an initializer method that returns 'this', and immediately calling that method:
No 'final' declaration needed.
int myVariable = 1;
myButton.addActionListener(new ActionListener() {
private int anonVar;
public void actionPerformed(ActionEvent e) {
// How would one access myVariable here?
// It's now here:
System.out.println("Initialized with value: " + anonVar);
}
private ActionListener init(int var){
anonVar = var;
return this;
}
}.init(myVariable) );
No 'final' declaration needed.
new ArrayList(10) { }