Monday, January 14, 2013

7 - Anonymous Inner Classes

1. Final formal parameter

Use the final modifier on formal paramaters to methods when you want to make it clear that the supplied value is not modified by that method.



Note also that using final on a reference variable does not mean that you can't change the value that the reference points to -- only that you won't change the reference itself.







Anonymous Inner Classes


An anonymous class in Java is a class with no specified name declared and instantiated at the same time.
Because it has no name it can only be used once at place of declaration.
Anonymous classes implement an interface or extend a class. They cannot declare their own constructors (they don’t have a name known to the programmer) so the constructor used for instantiation is one inherited from the superclass. In the case where the the anonymous class implements an interface a no parameter constructor inherited from java.lang.Object is used.
Access of Anonymous class
Anonymous classes are given a name by the compiler and they are treated as local inner classes. 
This means that anonymous classes can access members of the enclosing class regardless of their access modifiers and they can access final variables declared in the enclosing block of code.

The next example provides the basic syntax and shows how complexity can add up when the concept is “abused”;
01.package com.littletutorials.nested;
02. 
03.public class AnonymousTest
04.{
05.private String s = "test member access";
06. 
07.public void test(final String s)
08.{
09.// anonymous instance as a variable
10.Runnable r = new Runnable()
11.{
12.@Override
13.public void run()
14.{
15.System.out.print(getClass().getName() + " inner in ");
16.System.out.println(getClass().getEnclosingClass());
17.System.out.println("in anonymous class 1");
18.System.out.println(AnonymousTest.this.s);
19.}
20.};
21. 
22.Thread t1 = new Thread(r, "anonymous 1");
23. 
24.// anonymous instance as a parameter
25.Thread t2 = new Thread (new Runnable()
26.{
27.int a = 5;
28. 
29.@Override
30.public void run()
31.{
32.System.out.print(getClass().getName() + " inner in ");
33.System.out.println(getClass().getEnclosingClass());
34.System.out.println("in anonymous class 2");
35.System.out.println(s);
36. 
37.// anonymous instance inside another anonymous class
38.Thread t3 = new Thread (new Runnable()
39.{
40.@Override
41.public void run()
42.{
43.System.out.print(getClass().getName() + "inner in ");
44.System.out.println(getClass().getEnclosingClass());
45.System.out.println("in anonymous class 3");
46.System.out.println("a = " + a);
47.}
48. 
49.});
50.t3.start();
51.try
52.{
53.t3.join();
54.}
55.catch (InterruptedException e)
56.{
57.e.printStackTrace();
58.}
59.}
60. 
61.}, "anonymous 2");
62. 
63.try
64.{
65.t1.start();
66.t1.join();
67.t2.start();
68.t2.join();
69.}
70.catch (InterruptedException e)
71.{
72.e.printStackTrace();
73.}
74.}
75. 
76.public static void main(String[] args)
77.{
78.new AnonymousTest().test("final parameter");
79.}
80.}
The output of the execution reveals the naming conventions used by the compiler for anonymous classes:
com.littletutorials.nested.AnonymousTest$1 inner in class com.littletutorials.nested.AnonymousTest
in anonymous class 1
test member access
com.littletutorials.nested.AnonymousTest$2 inner in class com.littletutorials.nested.AnonymousTest
in anonymous class 2
final parameter
com.littletutorials.nested.AnonymousTest$2$1 inner in class com.littletutorials.nested.AnonymousTest$2
in anonymous class 3
a = 5

As you can see each anonymous class is numbered in the order of declaration in the enclosing class.
Usually anonymous classes are used for short non reusable implementations. Examples are runnable instances, listeners etc.
This post is part of a series explaining the Java concept of defining classes in other classes:

No comments:

Post a Comment