Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Is this a local anonymous class or a member anonymous class

Reply
Thread Tools

Is this a local anonymous class or a member anonymous class

 
 
Reporter
Guest
Posts: n/a
 
      05-12-2007
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);
}

}

 
Reply With Quote
 
 
 
 
Esmond Pitt
Guest
Posts: n/a
 
      05-12-2007
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.
 
Reply With Quote
 
 
 
 
Reporter
Guest
Posts: n/a
 
      05-12-2007
On May 11, 6:33 pm, Esmond Pitt <esmond.p...@nospam.bigpond.com>
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."





 
Reply With Quote
 
Mike Schilling
Guest
Posts: n/a
 
      05-12-2007
"Reporter" <> wrote in message
news: oups.com...
>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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Can Derived class static member access protected member from base class? Siemel Naran C++ 4 01-12-2005 06:46 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM
How would I use qsort to sort a struct with a char* member and a long member - I want to sort in order of the long member Angus Comber C Programming 7 02-05-2004 06:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57