File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner classes" Watch "inner classes" New topic

inner classes

rahul kumar
Ranch Hand

Joined: Feb 24, 2001
Posts: 75
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.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.

SCJP2. Please Indent your code using UBB Code
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
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

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).]
Jim Petersen
Ranch Hand

Joined: Jul 24, 2001
Posts: 241
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

- Jim Petersen <br />SCJP2<br />SCWCD<p>- but then again, I could be wrong...
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
I agree. Here's the link:
subject: inner classes
It's not a secret anymore!