final JTextArea textArea = new JTextArea(20, 50); JButton button1 = new JButton("Click me!"); button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { textArea.append("Hello!\n"); }});
class SomeName implements ActionListener { JTextArea textArea; SomeName(JTextArea textArea) { this.textArea = textArea; } public void actionPerformed(ActionEvent event) { textArea.append("Hello!\n"); }}
button1.addActionListener(new SomeName(textArea));
List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5); PrintStream out = System.out; Utils.forEach(nums, { Integer arg => out.println(arg); });
class SomeName implements javax.lang.function.VO { PrintStream out; SomeName(PrintStream out) { this.out = out; } public void invoke(Object arg) { invoke((Integer) arg); } public void invoke(Integer arg) { out.println(arg); } }
Utils.forEach(nums, new SomeName(out); }
Hello 1
,
Hello 2
, etc. for button clicksfinal int counter = 0; button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { counter++; textArea.append("Hello " + counter + "\n"); }});
counter
is final
.final int counter
to int counter
final int counter = 0
to final Integer
counter = new Integer(0)
final int counter = 0
to final int[]
counter = new int[1]
and counter++
to
counter[0]++
class Holder<T> { T value; }
final
restrictioniload_0 top → (value of local variable 0) | ... iconst_2 top → 2 | (value of local variable 0) | ...
imul top → (value of local variable 0)*2 | ...
javap
to decompile the first anonymous class file.
javap Lab7\$1
(The \
is necessary to escape the $
in bash;
omit if you use the DOS shell.)
What do you get? How does it compare with the second slide? (Note:
invokespecial
invokes a method that does not use the usual
dynamic dispatch on the implicit parameter. This is either a constructor
call, a call to super
, or a call to a private method.)
javap -c
Lab7\$1
in order to see the
bytecodes of the methods. Describe in plain English what the constructor
does.javap
to decompile the anonymous class file. What do you
get? How does it compare with the third slide?javap -c
. How does the curious +invoke
method get called? (No, I don't know why it is a separate method.)java -c
as necessary and explain which
strategy BGGA uses to mutate the counter
variable.