twc
Posts: 129
Nickname: twc
Registered: Feb, 2004
|
|
Re: Re-stated More Clearly: Anononymous Local Inner Class Question
|
Posted: Mar 6, 2004 11:11 PM
|
|
> I'm trying to understand the difference between anonymous > local inner classes that use the default constructor vs > those that don't. In example 1, the book says that the > local inner class piece translates into this...
Actually, you have several issues that are not related. Anonymous classes, inner classes, and default vs. non-default constructors. I've provided explanations for all three separately. The code in your example does involve all three, but there isn't anything (to my knowledge) important about the three being combined. If you understand them separately, you will be OK.
Anonymous classes: This is when you instantiate an object without specifying a name. (new SomeClass(); vs. SomeClass someName = new SomeClass();) This practice may be more common with inner classes, but it isn't limited to them. I frequently use anonymous classes in event handling and GUI's. For example, in GUI's it is common to create Panel objects that just fill up space so the other components line up right. Those are often instantiated anonymously since you don't really need to do anything with them.
inner classes: Classes that are defined within another class. Objects of that class can access the fields and methods of objects of the outer class. A common use is Event listeners that react when buttons are pushed, Enter is hit, etc..
default vs. non-default constructors: If you don't create a constructor for a class (inner or otherwise), then the compiler creates one for you that only does one thing - it calls the no-argument constructor for the parent of your class. If your class doesn't have a specified parent, then the Object class is its parent by default so its no-argument constructor gets called.
If you create one or more constructors - with or without arguments - then the compiler will not create a default constructor and you must call one of the constructors when you instantiate an object of that class.
I hope this helps. twc
BTW, you said you are a beginner. Just to Java, or to programming in general? I've never read Thinking In Java, but I've heard of it. What I have heard is that it is a great book for experienced progammers trying to learn Java, but NOT so good for raw beginners. If you are the latter, that might explain why you are getting confused. A lot of what you're asking about is NOT what a typical raw beginner would deal with.
|
|