The Artima Developer Community
Sponsored Link

Java Answers Forum
Local Inner Class Question

1 reply on 1 page. Most recent reply: Mar 6, 2004 2:28 PM by Kevin

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

Local Inner Class Question Posted: Mar 6, 2004 1:29 PM
Reply to this message Reply
Advertisement
I'm reading chapter 8 in Thinking In Java and am trying to understand the difference between 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...

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



My question, however, relates to example 2. What would the "translation" of the local inner class in that example be. Would it be the following?

MyWrapping implements Wrapping {
	public int value() {
		return super.value() * 47;
	}
}
 
return MyWrapping(x)




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

//: 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();
  }
} ///:~




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

//: 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);
  }
} ///:~

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


Kevin

Posts: 25
Nickname: doublek321
Registered: Jan, 2004

Re: Local Inner Class Question Posted: Mar 6, 2004 2:28 PM
Reply to this message Reply
Ugh, I left out the definition of class "Wrapping". Here it is below. By the way, the thing I'm really confused about is that, in example 1 (from my previous post), the book said that using a anonymous local inner class translated into code that implemented an interface. In this example, however, we're dealing with a class ("Wrapping"). I think that's a big piece of where my confusion is coming from.

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

Flat View: This topic has 1 reply on 1 page
Topic: Private Inner Class Question Previous Topic   Next Topic Topic: Is Iterator a Class ?

Sponsored Links



Google
  Web Artima.com   

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