Inner Classes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gmioannou
    New Member
    • Dec 2007
    • 8

    Inner Classes

    What is the need of inner classes?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Have a look at this example:

    [code=java]
    public class Star {

    private String name;

    public Star(String name) { this.name= name; }

    public class Planet {

    private String name;

    public Planet(String name) { this.name= name; }

    public class Moon {

    private String name;

    public Moon(String name) { this.name= name; }

    public String toString() { return name+" (orbiting "+Planet.this+" )"; }
    }

    public String toString() { return name+" (orbiting "+Star.this+")" ; }
    }

    public String toString() { return name; }

    public static void main(String[] args) {

    System.out.prin tln(new Star("sun").new Planet("earth") .new Moon("moon"));
    }
    }
    [/code]

    kind regards,

    Jos

    Comment

    • gmioannou
      New Member
      • Dec 2007
      • 8

      #3
      Thanks, Very helpfull...

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        A more prosaic example, from the Swing tutorial:

        Event Listener

        Comment

        Working...