• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

about anonymous inner class

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following statements are true?
A) An anonymous class cannot have any constructors
B) An anonymous class can only be created within the body of a method
C) An anonymous class can only access static fields of the enclosing class
D) An anonymous class instantiated and declared in the same palce.
who can tell me the correct answers and the reason?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I had same question too.
Check the discussion:
http://www.javaranch.com/ubb/Forum24/HTML/003895.html
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A) An anonymous class cannot have any constructors
My ans: TRUE
Constructors have same name as that of the class. Now, anonymous classes have no name and hence u do not provide any constructors for it. In fact, the compiler actually creates a constructor behind the scenes.
B) An anonymous class can only be created within the body of a method
My ans: FALSE
Actually an anonyous class declaration is always associated with a class instance creation. i.e.
class Test
{
Button B = new Button(){ --override some method/do something-};
}
Out here, u can see an anonymous class is created and it overrides some method in the Button class. Further, the anonymous class declaration here is associated with the Button b instance creation.
C) An anonymous class can only access static fields of the enclosing class
My ans: FALSE
Remember that anonymous class are basically inner class(mind u NOT static inner classes). Therefore, inner classes cannot mark a variable static. It can do so provided they are marked final.
For eg:-
class UnrelatedClass
{
static int k = 100;
}
class Outer
{
class Inner extends UnrelatedClass
{
static int i = 10; // compiler error!
static final int i = 10; // compiles fine
void print()
{
System.out.preintln("Value K is inherited and it is:"+ k);
}
}
}
Also, note that an inner class can inherit static member as shown in above example but not declare them. They can do so provided they are marked final.
D) An anonymous class instantiated and declared in the same palce.
My ans: TRUE
They are declared and instantiated in the same place. Write a small piece of code and u'll see this point.
Also, remember the following:
Due to point (D) and since anonymopus classes are associated with class instance creation, therefore anonymous classes are NEVER ABSTRACT OR STATIC. Plus they IMPLICITLY FINAL.
Hope this helps :-)
-sampaths77
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dilip,
I went thro the link u mentioned. U had quoted a sentence sayiing that u can have anonymous class within a method - Could someone please explain that.
I really can't think how this can de done. Could some please clarify this for me???
Thanks a million
-sampath77
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic