Java Reference
In-Depth Information
20. x.radius = 10;
21. x.doSomething();
22. }
23.}
The doSomething method contains a local inner class named Circle . The Circle class
declares a method named area that refers to the pi variable from line 6, which is only valid
because pi is final . Circle also refers to radius on line 10, which is valid because radius is a
fi eld of the outer class. The code compiles fi ne, the value of radius is 10, so the output is
314.15000000000003
By the way, compiling LocalInner.java creates two bytecode fi les: LocalInner.class
and LocalInner$1Circle.class . The compiler adds a 1 to the name of the Circle class
because it is possible that a different method in the class defi nes another local class named
Circle .
Precision of Doubles
I didn't mean for the local inner class example to demonstrate an issue with the precision
of doubles, but because the output of LocalInner is slightly unusual, I probably
should clarify the result. The product of 3.1415 * 10 * 10 is 314.15 , but the output is
314.15000000000003 . This is because double values are not exact. They are stored in
64 bits using the IEEE standard 754, which is an accurate technique for representing a
fl oating-point number as a sequence of 1s and 0s, but the values are not entirely exact.
You won't see a question about this on the exam, but it is good information to know. Visit
the IEEE website at www.ieee.org if you are interested in delving into this topic further.
Anonymous Inner Classes
An anonymous inner class is a local inner class that does not have a name. It is declared
and instantiated all in one statement using the new keyword. Anonymous inner classes
either extend an existing class or implement an existing interface.
For example, the following statement declares and instantiates a new class that is a child
of Thread . The anonymous inner class defi nition starts on line 6 and ends with the right
curly brace on line 12. The semicolon on line 12 denotes the end of the new statement.
Search WWH ::




Custom Search