The Artima Developer Community
Sponsored Link

Java Answers Forum
Re-stated More Clearly: Anononymous Local Inner Class Question

1 reply on 1 page. Most recent reply: Mar 6, 2004 11:11 PM by twc

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Kevin

Posts: 25
Nickname: doublek321
Registered: Jan, 2004

Re-stated More Clearly: Anononymous Local Inner Class Question Posted: Mar 6, 2004 2:52 PM
Reply to this message Reply
Advertisement
My question pertains to anonymous local inner classes (in chapter 8 of Thinking In Java).

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...

--------------------------------------------------------------
THE BOOKS' TRANSLATION OF THE ANONYMOUS LOCAL INNER CLASS IN EXAMPLE 1

class MyContents implements Contents {
  private int i = 11;
  public int value( ) { return i; }
}
return new MyContents( );

--------------------------------------------------------------


My question relates to example 2, however. I tried to follow the logic of the "translation" done in example 1 and came up with a guess as to what the translation would look like for example 2 (immediately below). If my translation is correct (which I'm guessing it isn't) then it's bizaare because it's trying to implement a class ("Wrapping") instead of an interface. Where is my logic going wrong?


-------------------------------------------------
MY GUESS AT THE TRANSLATION OF THE ANONYMOUS LOCAL INNER CLASS IN EXAMPLE 2
MyWrapping implements Wrapping {
	public int value() {
		return super.value() * 47;
	}
}
 
return MyWrapping(x)

-------------------------------------------------



-------------------------- -----
EXAMPLE 1:

public interface Contents {
  	int value();
}



//: c08:Parcel6.java
// A method that returns an anonymous inner class.
 
public class Parcel6 {
  public Contents cont() {
    return new Contents() {
      private int i = 11;
      public int value() { return i; }
    }; // Semicolon required in this case
  }
  public static void main(String[] args) {
    Parcel6 p = new Parcel6();
    Contents c = p.cont();
  }
} ///:~

-------------------------------
EXAMPLE 2

public class Wrapping {
	private int i;
	public Wrapping(int x) { i = x; }
	public int value() { return i; }
}


//: c08:Parcel7.java
// An anonymous inner class that calls
// the base-class constructor.
 
public class Parcel7 {
  public Wrapping wrap(int x) {
    // Base constructor call:
    return new Wrapping(x) { // Pass constructor argument.
      public int value() {
        return super.value() * 47;
      }
    }; // Semicolon required
  }
  public static void main(String[] args) {
    Parcel7 p = new Parcel7();
    Wrapping w = p.wrap(10);
  }
} ///:~

--------------------------------------


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
Reply to this message Reply
> 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.

Flat View: This topic has 1 reply on 1 page
Topic: Package Previous Topic   Next Topic Topic: Exception problem about OutputStream already obtained

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use