Results 1 to 2 of 2
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: Inner Classes...

  1. #1
    Splanger is offline Member
    Join Date
    Jul 2011
    Posts
    10
    Rep Power
    0

    Default Inner Classes...

    Hello, again)

    In the Oracle's tutorial of the Inner Classes, it says:

    To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax:

    OuterClass.InnerClass innerObject = outerObject.new InnerClass();
    and after that, they bring an example with some program, which has inner class - array iterator.
    here is itself.

    But I can't find there any sign of the Inner Class Constructor...
    So, how the inner class can be instantiated, if it has no constructor? Which constructor becomes default for it?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    The inner class is InnerEvenIterator. It has a constructor, all classes do. If there no constructor explicitly given in the code then a no argument constructor that does nothing is assumed.

    The use of the constructor is in the printEven() method:

    Java Code:
    public void printEven() {
        //print out values of even indices of the array
        [color=blue]InnerEvenIterator iterator = this.new InnerEvenIterator();[/color]
        while (iterator.hasNext()) {
            System.out.println(iterator.getNext() + " ");
        }
    }
    The differences are that the outer class is not named explicitly ie it is "InnerEvenIterator iterator = ..." instead of "DataStructure.InnerEvenIterator iterator = ...". That's because the printEven() method is actually part of the DataStructure class. Also "this" is used where the example code has "outerObject": in printEven() this is the outer object (and it is the only variable there is for this object).
    sunde887 likes this.

Similar Threads

  1. Help with classes
    By Exothesis in forum New To Java
    Replies: 11
    Last Post: 10-11-2010, 03:00 AM
  2. classes
    By exose in forum New To Java
    Replies: 2
    Last Post: 09-30-2010, 05:21 PM
  3. A little help with classes.
    By ThrashingBoy in forum New To Java
    Replies: 8
    Last Post: 06-10-2010, 10:52 AM
  4. Cant run my classes
    By Assaf A in forum Eclipse
    Replies: 1
    Last Post: 04-22-2008, 03:31 PM
  5. Using a JAR from other classes
    By Joe2003 in forum Advanced Java
    Replies: 1
    Last Post: 01-02-2008, 08:08 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •