Anonymous class - I don't know how to...



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

.



Relevant Pages

  • Re: Anonymous class - I dont know how to...
    ... Syntax for creating anonymous class: ... If we declare class C inside method A, ... public void A (final int i) { ... public int lexicalContext { ...
    (comp.lang.java.programmer)
  • Re: Anonymous class - I dont know how to...
    ... The above statement creates an object of anonymous class and returns ... If we declare class C inside method A, ... The reasons are perhaps more technical than anything else. ... the current fundamental difference between local variables and class ...
    (comp.lang.java.programmer)
  • Re: non-static method gotcha
    ... the declared enum, 'SubmissionSite' in your example. ... Its effect is to declare that C is not an inner class. ... extends the immediately enclosing enum type. ... decompiled anonymous class. ...
    (comp.lang.java.help)
  • Re: Anonymous class - I dont know how to...
    ... The above statement creates an object of anonymous class and returns ... If we declare class C inside method A, ... public void A (final int i) { ...
    (comp.lang.java.programmer)
  • Re: "cannot find symbol" on menuItem for ActionListener
    ... there's no way to declare a _static_ nested ... I think that anonymous classes are always inner ... Anonymous classes defined in static methods are effectively static ... anonymous class defined in an instance method won't access its parent ...
    (comp.lang.java.programmer)