Is this a local anonymous class or a member anonymous class

Discussion in 'Java' started by Reporter, May 12, 2007.

  1. Reporter

    Reporter Guest

    I read that an anonymous class is an unnamed local class.

    However, in the example below, I have created what I believe to be an
    anonymous class that is a member class, not a local class. Anyone
    want to comment?




    package anonymousexample3a;

    public class Main {

    ClassA anonB_Object = new ClassA("Hello" ) {
    // The original Display Method
    public void Display(){
    System.out.println("Inherited Anonymous Overridden MEMBER
    Display ClassA Output:"+Message);
    }
    };

    public Main() {
    ClassA classA_Object = new ClassA("Hello");

    // Display the Anonymouse Version
    classA_Object.Display();

    ClassA anonA_Object = new ClassA("Hello" ) {
    // The original Display Method
    public void Display(){
    System.out.println("Inherited Anonymous Overridden
    LOCAL Display ClassA Output:"+Message);
    }
    };

    // Display the Anonymouse Class Based on ClassA
    anonA_Object.Display();

    anonB_Object.Display();
    }

    public static void main(String[] args) {
    Main example = new Main();

    }
    }



    class ClassA {
    String Message = "";
    public ClassA(String aMessage){
    Message = aMessage;
    }

    // The original Display Method
    public void Display(){
    System.out.println("Basic ClassA Output:"+Message);
    }

    }
    Reporter, May 12, 2007
    #1
    1. Advertising

  2. Reporter

    Esmond Pitt Guest

    Reporter wrote:
    > I read that an anonymous class is an unnamed local class.


    Where?

    > However, in the example below, I have created what I believe to be an
    > anonymous class that is a member class, not a local class. Anyone
    > want to comment?


    Well it's not a member class, it is? It's a member *variable* which is
    an instance of an anonymous class.

    An anonymous class is an unnamed class. By definition. Whether the
    *instance* is local or a member has nothing to do with what the class is.
    Esmond Pitt, May 12, 2007
    #2
    1. Advertising

  3. Reporter

    Reporter Guest

    On May 11, 6:33 pm, Esmond Pitt <>
    wrote:
    > Reporter wrote:
    > > I read that an anonymous class is an unnamed local class.

    >
    > Where?
    >
    > > However, in the example below, I have created what I believe to be an
    > > anonymous class that is a member class, not a local class. Anyone
    > > want to comment?

    >
    > Well it's not a member class, it is? It's a member *variable* which is
    > an instance of an anonymous class.
    >
    > An anonymous class is an unnamed class. By definition. Whether the
    > *instance* is local or a member has nothing to do with what the class is.


    Java in a Nutshell
    Third Edition

    by David Flanagan

    Chapter 3: Object-Oriented Programming in Java
    3.12. Anonymous Classes
    "An anonymous class is a local class without a name."
    Reporter, May 12, 2007
    #3
  4. "Reporter" <> wrote in message
    news:...
    >I read that an anonymous class is an unnamed local class.
    >
    > However, in the example below, I have created what I believe to be an
    > anonymous class that is a member class, not a local class. Anyone
    > want to comment?


    Let's see. A member class is defined within the scope of its containing
    class Depending on its protection level, it might be visible within the
    class, the package, or the world. A local class is defined within a method
    and is visible only within that method. Of course, since anonymous class
    have no names, you can't sensibly discuss what scope they're defined in.

    You seem to have found an edge case. I suggest that since your

    public class Main {

    ClassA anonB_Object = new ClassA("Hello" ) {
    // The original Display Method
    public void Display(){
    System.out.println("Inherited Anonymous Overridden MEMBER
    Display ClassA Output:"+Message);
    }
    };

    is the moral equivalent of

    public class Main {

    ClassA anonB_Object;

    init {
    aonB_Object = new ClassA("Hello" ) {
    // The original Display Method
    public void Display(){
    System.out.println("Inherited Anonymous Overridden MEMBER
    Display ClassA Output:"+Message);
    }
    }
    };

    that your anonyomus class is local to the (implied) init block. I don't
    insist in this, but I think it's simpler than defining yet another kind of
    inner class.
    Mike Schilling, May 12, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,755
    Thomas Weidenfeller
    Oct 12, 2005
  2. Mike Schilling
    Replies:
    6
    Views:
    400
    Roedy Green
    Nov 17, 2005
  3. Siemel Naran
    Replies:
    4
    Views:
    805
    Micah Cowan
    Jan 12, 2005
  4. Replies:
    22
    Views:
    691
    CBFalconer
    Sep 30, 2007
  5. Replies:
    1
    Views:
    220
Loading...

Share This Page