Dear all.
Unfortunately I haven't coded Java for about five years and I absolutely can not remember how or why the following code is working.
I stumbled across a similar example and broke it down to this. The emphasis is on the part below the comment: I don't get the constructor notation followed by the block in double brackets. And unfortunately I can not find anything in the java doc or by using google (what word(s) should I google?).
package syntaxtest;
public class Main {
public static void main(String[] args) {
// What kind of notation is this:
MyTest tester = new MyTest() {{
setName("John Johnson");
}};
System.out.println(tester.getName());
}
}
class MyTest {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
So here are my questions:
1 How is this notation/ syntax called?
2 Where can I read some documentation about it?
I guess/ hope I will be able to answer the second question by myself if somebody can provide me with the answer to the first question.
To make it clear: I know the output is 'John Johnson'! ;) But I don't know why it is working!
However: any help is appreciated and many thanks in advance!
Yours.
dennis