help creating instance of nested class

Luis Casillas luis@casillas.org
Wed Jan 30 04:22:00 GMT 2008


On Jan 29, 2008, at 7:37 PM, John Whittaker wrote:

> I neglected to add that
> javolution.io.Struct.Unsigned16 is a non-static,
> nested class of the Struct class.  I don't know if
> that makes any difference or not in this discussion.
> I've just been wrestling a bit with what must occur
> here when trying to create an instance of the
> Unsigned16.  A nested, non-static class has to be
> created "from" or "with" an instance of the enclosing
> class, right?  (I'm not even sure about that).  And in
> Kawa, what would the correct Scheme form be to do
> that?


That's gotta be part of the problem you're facing (though judging on  
the error message you're seeing, I doubt it's the whole story).

The normal way to instantiate a non-static member class is to do so  
within an instance method of an enclosing class (a method that gets  
passed a "this" reference when invoked, which then becomes the  
enclosing class "this" reference for the inner class instance).

I was going to offer the guess that you can't instantiate a non- 
static inner class other than from such an instance method, but I  
found the following article:

http://www.onjava.com/pub/a/onjava/excerpt/HardcoreJava_chap06/ 
index.html

...which shows what's got to be the most obscure Java syntax I've  
ever seen, precisely for doing that:

       MonitorScreen screen = new MonitorScreen( );
       MonitorScreen.PixelPoint pixel = screen.new PixelPoint(25, 40);

If I'd seen that "screen.new PixelPoint(25, 40)" in some code, I  
wouldn't have understood it.

So now the question is how to do the same within Kawa.  The Kawa  
documentation doesn't address this specific case, but a Java non- 
static inner class is compiled  as a separate class file, with the  
difference that all the constructors are implicitly modified to take  
an extra argument, representing the enclosing instance.  So, the  
example just shown above *might* work the following way in Kawa  
(untested), by passing the "hidden" argument explicitly to the inner  
class constructor:

  (let* ((screen (<oreilly.hcj.nested.MonitorScreen>:new))
         (pixel (<oreilly.hcj.nested.MonitorScreen$PixelPoint>:new  
screen 25 40)))
    ;; do stuff...
    )

To tell you the truth, I suspect that this will be necessary to get  
the inner class instantiation to work, but perhaps not sufficient.



More information about the Kawa mailing list