File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner Classes" Watch "Inner Classes" New topic
Author

Inner Classes

amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231

Hi everybody,
I have 4 questions:
Q1) Is nested top-level class always static? Can it access private fields of the enclosing class ?
Q2) Is member inner class always non-static? Can it contain non-static members?
Q3) Can member inner classes access stayic members of the enclosing class?
Q4) Can Anonymous inner classes extend any other class or implement an interface in its declaration?

amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231

I am sorry. The 2nd question is:
Is member inner class always non-static? Can it access static members of enclosing class?
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3315
    
    8
Q1) Is nested top-level class always static? Can it access private fields of the enclosing class ?
Yes. There are two types of Top Level classes. Package level classes and Nested ones. And Nested Top-level classes are actually static inner classes. Non-static inner classes are simply termed as inner classes.

Q2) Is member inner class always non-static? Can it contain non-static members?
Yes. It's just the terminology. Oother wise, of course, static inner classes are also "inner" classes.
Q3) Can member inner classes access stayic members of the enclosing class?
Yes.
Q4) Can Anonymous inner classes extend any other class or implement an interface in its declaration?
Yes. Search the posts for "anonymous classes" You'll find a lot of examples.

//Inserted later on
No, if "in it's declaration" means explicitly putting extends or implements clause. Isn't name of the class is a part of it's declaration?
HTH,
Paul.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

[This message has been edited by Paul Anil (edited December 12, 2000).]


Enthuware - Best Mock Exams and Questions for Oracle/Sun Java Certifications
Quality Guaranteed - Pass or Full Refund!
manav kher
Ranch Hand

Joined: Nov 21, 2000
Posts: 98
i thought anonymous inner classes already extended object and implemented interface and thats why cannot have extend or implement anything..so the answer to Q4 should be false
manav
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
One more note to Paul's great explanations,
Anonymous inner classes MUST extend another class OR implement an interface by defination. When you declare an anonymous inner class in a method you say:
aMethod(new ActionListener(){}); you are really saying anonymouse class implements ActionListener.
and if you say:
aMethod(new ComponentAdapter(){}); you are really saying anonymous class extends ComponentAdapter.
An anonymous class cannot extend a class and implement an interface at the same time. If it implements an interface, then there is the implicit extending of class Object. Also, if you extend a class you can pass arguments while if you implement an interface you cannont since class Object doesn't have a constructor that takes arguments.
Bill
[This message has been edited by bill bozeman (edited December 12, 2000).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Inner Classes