On Sat, Apr 30, 2005 at 12:15:49AM +0900, Austin Ziegler wrote:
> On 4/29/05, David Holroyd <ruby-talk / badgers-in-foil.co.uk> wrote:
> > On Fri, Apr 29, 2005 at 10:26:38PM +0900, Austin Ziegler wrote:
> >> What he's saying is what I said, in part. In PDF::Writer,
> >> recently, I did the following:
> >>
> >>   class SimpleTable
> >>     class Column
> >>       ...
> >>     end
> >>     ...
> >>   end
> >>
> >> To do this in Java -- especially since Column is accessible as
> >> SimpleTable::Column (it's a full blown class, not just a nested
> >> class, if that's even possible in Java -- it's been a long time)
> > FYI, the Java version of this construct would look something like,
> > 
> >   public class SimpleTable {
> >       public static class Column {
> >           ...
> >       }
> >       ...
> >   }
> 
> Is that the same? You've got:
> 
>   public class SimpleTable
>   public static class Column
> 
> What effect does the "static" have on SimpleTable::Column in Java?

With the above definition, one could say,

  SimpleTable.Column col = new SimpleTable.Column();

Without the 'static', instances of the inner class are associated (I
forget the appropriate terminology) with instances of the outer class.

This means for client code to construct a Column, it needs,

  SimpleTable tab = new SimpleTable();
  SimpleTable.Column col = tab.new SimpleTable.Column();

However, now, methods of Column can access member variables of the outer
SimpleTable class instance (the compiler synthesises an additional,
hidden 'this' reference in non-static inner classes, IIRC).

(The syntax *is* simpler when constructing a Column from an instance
method of the outer class.)


So, static inner classes behave the same as top-level classes, and
non-static inner classes exist to confuse the unwary.


dave

-- 
http://david.holroyd.me.uk/