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



kaja_love160@xxxxxxxxx <kaja_love160@xxxxxxxxx> wrote:
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 ), ...

.... you'd not only define the variable, but also overwrite
a (non-final, of course) method from the base-class, which then
accesses the variable.

e.g.
Object o = new Object() {
public int anon = 20;
// doesn't make much sense except for the sake of an example:
public String toString() { anon++; return Integer.toString(anon); }
}
o.toString(); // -> 21
o.toString(); // -> 22

Alternatively, you can also use the reflection-API to
see and access the variable "anon" of the object referenced by "o".

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 ; }
}

The reasons are perhaps more technical than anything else.
At class-file level, C is in a separate class-file than the
class containing A, and it would be quite complicated to implement
a connection between C's "i", and the local variable "i" in A.
The same restriction holds currently, if "i" were a field of the
class that contains "A", and my feeling is, that this case is
slightly more likely to change in future than the case of a local
variable.

In a nutshell, I consider it a current limitation of implementation
that was then taken into java language spec for practical reasons.
I wouldn't bet that this requirement will be there into all eternity.
At some point in future, some brilliant programmer might find a way
to actually implement a live connection between these vars, despite
the current fundamental difference between local variables and class
variables, and the requirement for "final" might then be dropped.

PS: I'm sure, some will followup with their personal decision, that
they indeed would bet on this limitation never ever changing, and
I wouldn't bet against them, either. I'm not really the betting
type :-)

.



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: 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)