• 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

inner classes

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why doesn't this gives a compilation error ... at line one we are trying to create an object of the interface ....
why is there a nullpointer exception at line 2

[ July 24, 2005: Message edited by: Barry Gaunt ]
[ July 24, 2005: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In 'suncerti' class, the variable 'a' will be null until you call the 'aMethod', as the code to assign a new object to 'a' is present only in 'aMethod'
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At line 1 your are not "trying to create an object of the interface."

At line 1 you are instantiating an object of a class that implements the interface. The class is an anonymous class (that is, has no name) and it provides an implementation of the interface A's innerMeth method.

The previous post explains the NullPointerException.
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At line 1, we are creating an anonymous inner class (within method void) which implements interface A. This is not same as trying to instantiate a toplevel interface (which gives a compilation error).
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi vineet babu
how can you explain that A is anonymous class not interface.???

How do you get the compilation error when it is Toplevel interface.

How does the whole code work without any errors or exceptions???
 
naraharirao mocherla
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi vineet babu
how can you explain that A is anonymous class not interface.???

How do you get the compilation error when it is Toplevel interface.

thanx
 
raghu babu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you were to say
a = new A();
this means you are trying to instantiate interface A.

a = new A(){

watch the above syntax carefully, you didn't end the above
statement with a semi-colon and trying to provide more statements which
means you are creating an object that belongs to anonymous inner class and this anonymous inner class implements interface A.

For a detailed understanding of the anonymous inner class, I recommend
going thru any JAVA programming book that has innerclass chapter.

Hope this helps.
reply
    Bookmark Topic Watch Topic
  • New Topic