/**
* This code is from the book:
* Winder, R and Roberts, G (1998)
* Developing Java Software</em>
* John Wiley & Sons.
* It is copyright (c) 1997 Russel Winder
* and Graham Roberts.
*/
interface Thing{
public void test();
}
class l1Local2{//Class9
private String name = "l1Local2";
public Thing f(final String h, String w){
int j = 20;
final int k = 30;
class Local implements Thing{
public void test(){
//OK as h is final
System.out.println(h);
//Cannot do this as w is not final
//System.out.println(w); //Error
//Cannot do this as j is not final
//System.out.println(j); //Error
//OK k is final
System.out.println(k);
//Cannot do this as i is not yet declared
//System.out.println(i); //Error
//Like a member class, instance variables of
//the enclosing object can be accessed.
//They don't need to be final.
System.out.println(name);
}
}
Local l = new Local();
final int i = 10;
return l;
}
public static void main(String[] args){
l1Local2 c9 = new l1Local2();
//Get a reference to a local class object
//conforming to type Thing
Thing t = c9.f("hello", "world");
//Call a method of the local class object to
//verify its state
t.test();
}
}
/******** sample compilation & run *******
# javac l1Local2.java
# java l1Local2
hello
30
l1Local2
#
******************************************/