K
kaja_love160
hello
1) Syntax for creating anonymous class:
new < superclass> ( <params> ) { <class definition> };
Example:
Object o = new Object() { public int anon = 20; };
The above statement creates an object of anonymous class and returns
a reference to it. But in order to use members declared in anonymous
class ( o.anon ), variable o would have to be of same type as
anonymous class. How do we make 'o' of same type, considering that
anonymous class has no name?
2)
If we declare class C inside method A(), then any local variable
( declared inside A() ) that will also be used inside class C, must be
declared final. Why? I assume there must be some valid reason for
that?
public void A ( final int i ) {
class C {
int u = i ;
}
}
thank you for your help
1) Syntax for creating anonymous class:
new < superclass> ( <params> ) { <class definition> };
Example:
Object o = new Object() { public int anon = 20; };
The above statement creates an object of anonymous class and returns
a reference to it. But in order to use members declared in anonymous
class ( o.anon ), variable o would have to be of same type as
anonymous class. How do we make 'o' of same type, considering that
anonymous class has no name?
2)
If we declare class C inside method A(), then any local variable
( declared inside A() ) that will also be used inside class C, must be
declared final. Why? I assume there must be some valid reason for
that?
public void A ( final int i ) {
class C {
int u = i ;
}
}
thank you for your help