• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

anonymous class doubt

 
Ranch Hand
Posts: 274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the following question:
Which of the following are true statements?

a. An anonymous class is implicitly abstract.
b. An anonymous class is implicitly final.
c. An anonymous class is implicitly static.
d. A static reference variable can reference an instance of an anonymous class.
e. An anonymous class declaration must have at least one explicit constructor declaration.
f. An anonymous class declaration can have more than one explicit constructor declaration.

Answer:b,d

I'm confused with answer D. How can we create a A static reference variable can reference an instance of an anonymous class?

Please help.

Gitesh
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Anonymous class means anonymous inner class: Anonymous inner class comes in two flavours, one that extends another class and other that implements an interface.

d. A static reference variable can reference an instance of an anonymous class.
Yes you can do this. For that matter you can have a static refernce variable that can refer to an instance of any type of class
Try this code
class A {
static B bStatic = new B();
}

class B {}
It only means that bStatic is a static member of class A.
and you can also have
class A {
static B bStatAnnony = new B() { }; // Here bStatAnnony points to an instance of a class that extends B but it has no name
}

Hope this clears the confusion.
Thanks
Deepak
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For example,

you create an anonymous class

Foo f= new Foo(){};

so a static reference variable refers to an instance of an anonymous class will be:

static Foo sf;
sf=f

hope that helps
 
reply
    Bookmark Topic Watch Topic
  • New Topic