• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

inner classes

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Which of the following stmt.s regarding inner classes are true?
1) A non-static inner class may have static members.
2) Anonymous inner classes cannot have 'extends' and 'implements' clause.
3) Anonymous inner classes can only be created for interfaces.
4) Anonymous inner classes can never have initialization parameter.
5) Anonymous inner classes cannot be static .
What is the answere ? with detailed explanation.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2) Anonymous inner classes cannot have 'extends' and 'implements' clause.
new BaseClass() {};//A
new ImplementedInterface () {};//B
A creates an anomymous class that extends from BaseClass. Compiler won't accept any extends (not needed) or implements
B creates an anonymous class that implements ImplementedInterface. Compiler wont't accept any extends or implements (not needed)
4) Anonymous inner classes can never have initialization parameter.
4.1)new BaseClass(arg, ..., arg) {}
This calls the constructor BaseClass(arg, ...., arg)
4.2)Another way of initializing an anonymous class

Note the use of a initialization block, and that the parameter to the method must be final to be accessed from the local class
5) Anonymous inner classes cannot be static .
I am not very sure about the explanation:
Given that there is no name you can't place static before it. The same for extends or implements.
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm lets see
Answers are
2. Anonymous inner class (they are defined and created at the same place, so there is no extends or implements... Instead
just use new superclass(), or new superinterface())
But if the superclass doesnt have a default constructor,
then it will be new superclass(args))
4. Anonymous class can never have initialization parameters
Thankx
Ragu

Note: anonymous class can be static
But the catch is they CANT be declared as static. But if the context is static (ie for ex: static method) then they invariably become static

[This message has been edited by Ragu Sivaraman (edited September 29, 2001).]
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
1. A non static inner class can't have static members. As non static classes can only refer to instances of the class not the class itself
5. Anonymous inner classes can be implicitly static given the context of the defining method ie if the enclosing method is static then anonymous class would also be static (although the static keyword cannot be applied to anon inner class). An example of this would be a static anon inner class that returns a value of a static method.
Im sure some others round here can exapnd on these
hope this helps some Jim
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ragu and Jim I have checked that anon classes can be static.
to Ragu:
what dou you think of my points 4.1 and 4.2. Aren't they initialization parameters.
to Jim:
An inner class (not static) can also refer to the class itself

I think is similar to an instance method accessing a static one.
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Re: 4 False
An anonymous class uses the constructors available for the class it is extending. If that class has ctors which take parameters then the anonymous class can take parameters.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
reply
    Bookmark Topic Watch Topic
  • New Topic