• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner Classes

 
amit sanghai
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3714
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
manav kher
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic