Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the keyword 'this' needed when instantiating a new inner class?

Tags:

java

Another example from the Oracle Java SE tutorials. It works fine, but I'm not sure if/why 'this' is necessary when creating an instance of the inner class. The result seems to be same regardless of whether I take it out or not.

To be clear, I am referring to: InnerEvenIterator iterator = this.new InnerEvenIterator(); // not sure why using 'this'

public class DataStructure {
    // create an array

    private final static int SIZE = 15;
    private int[] arrayOfInts = new int[SIZE];

    public DataStructure() {
        // fill the array with ascending integer values
        for (int i = 0; i < SIZE; i++) {
            arrayOfInts[i] = i;
        }
    }

    public void printEven() {
        // prints out the value of even indices in the array
        InnerEvenIterator iterator = this.new InnerEvenIterator(); // not sure why using 'this'
        while (iterator.hasNext()) {
            System.out.println(iterator.getNext() + " ");
        }
    }

    // inner class implements the Iterator pattern
    private class InnerEvenIterator {
        // start stepping through the array from the beginning

        private int next = 0;

        public boolean hasNext() {
            // check if a current element is the last in the array
            return (next <= SIZE - 1); // -1 b/c dealing with array's length.
        }

        public int getNext() {
            // record a value of an even index of the array
            int retValue = arrayOfInts[next];
            // get the next even element
            next += 2;
            return retValue;
        }
    }

    public static void main(String s[]) {
        // fill the array with integer values and print out only
        // values of even indices
        DataStructure ds = new DataStructure();
        ds.printEven();
    }
}
like image 777
CodeFinity Avatar asked Aug 19 '13 03:08

CodeFinity


People also ask

How do you instantiate an inner class?

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 outerObject = new OuterClass(); OuterClass. InnerClass innerObject = outerObject.

How do you create an inner class in Java?

Creating an inner class is quite simple. You just need to write a class within a class. Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class.

What is inner class keyword?

Inner classes can be either static or non-static. A static inner class is one that is declared with the static keyword. A non-static inner class is one that is not declared with the static keyword. We will be looking at how to work with Inner Classes in this Java programming tutorial.

What is the correct syntax for member inner class?

It is also known as regular inner class. It can be declared with access modifiers like public, default, private, and protected. The syntax to declare member inner class in Java is like this: Syntax: class Outer { // A member inner class named Inner.


2 Answers

You don't need a this., in this case. However, an inner class must always be constructed using an enclosing instance, so the this. simply makes this clear to the reader.

If you're not inside of a non-static method of DataStructure (i.e., if this does not exist, or else isn't an instance of DataStructure), then you must actually specify an instance of DataStructure when creating an InnerEvenIterator:

InnerEvenIterator iterator = dataStructure.new InnerEvenIterator();
like image 111
Chris Jester-Young Avatar answered Oct 01 '22 01:10

Chris Jester-Young


Just to add a point to correct answers mentioned above:

When you have a function, let's say setInnerEvenIterator(InnerEvenIterator), as follows

class DataStructure {

    InnerEvenIterator iterator;

    public void setInnerEvenIterator(InnerEvenIterator iterator) {
        this.iterator = iterator;
    }
}

then you need to specify this keyword to rectify the possible conflict that may arise due to same argument and instance name. So this.someThing represents instance variable of the class. If you do not explicitly provide one it is OK as long as there is no conflicting situation arising out of it.

like image 34
Aniket Thakur Avatar answered Oct 01 '22 00:10

Aniket Thakur