0

Today I studied Iterator pattern, but I did not quite understand a pice of code. Could you help me with it?

Here is a class:

public class Repository implements Container{
    public String[] names = {"John", "Iren", "Anthony", "Lorn"};

    @Override
    public Iterator getIterator() {
        return new MyIterator();
    }
    private class MyIterator implements Iterator {
        int index;
        @Override
        public boolean hasNext() {
             if (index < names.length) {
                 return true;
             }
             return false;
        }

        @Override
        public Object next() {
           if (this.hasNext()) {
               return names[index++];
           }
           return null;
        }
    }
}

And the main method:

public static void main(String[] args) {
        Repository name = new Repository();
        for (Iterator iter = name.getIterator(); iter.hasNext(); ) {
            String local = (String) iter.next();
            System.out.println("Name = " + local);
        }
    }

The question is about method next() :

        @Override
        public Object next() {
           if (this.hasNext()) {
               return names[index++];
           }
           return null;
        }

I don`t understand the meaning of keyword in this context. This is reference for what?

3 Answers 3

1

The this keyword is the reference to the object which non-static method you are inside. Here this is inside the next() method of the MyIterator object, thus this is the reference to the MyIterator object. Note that in the supplied code you can omit this. and write simply if(hasNext()) {...}.

Sign up to request clarification or add additional context in comments.

Comments

0

this is a reference variable that refers to the current object

Within an instance method or a constructor, this is a reference to the current object — the object whose method or constructor is being called. You can refer to any member of the current object from within an instance method or a constructor by using this.

Have a look Java doc and here

Comments

0

this keyword in Java is a special keyword which can be used to represent current object or instance of any class in Java. “this” keyword can also call constructor of same class in Java and used to call overloaded constructor.

"this" sometime also associate with super keyword which is used to denote instance of super class in Java and can be used to call overloaded constructor in Java.

--> this keyword represent current instance of class.



--> this keyword can be used to call overloaded constructor in java. if used than it must be first statement in constructor this() will call no argument constructor and this(parameter) will call one argument constructor with appropriate parameter. here is an example of using this() for constructor chaining:


Example


"this" keyword to call constructor in Java

class Student{
    private int id;
    private String name;

    public Student(){
       this(“Student of the year”);
    }

    public Student(int id){
        this.id = id;
        this.interest = 0.0;
    }  
}


If member variable and local variable name conflict, this can be used to refer member variable.

Example #2

  public Student(int id, double name){
        this.id = id;
        this.name = name;
  }


this is a final variable in Java and you can not assign value to this. 

this = new Student(); //this will result in compilation error--cannot assign value to final variable : this

Or you can call methods of class by using this keyword 
Example #3

   public String getName(){
        return this.toString();
    }

Or this can be used to return object. this is a valid return value. 
Example #4

public Student getStudent(){
 return this;
}

In your case, I think "this" can represent your current Object Repository contains String array names that has John, Iren, Anthony, Lorn as word/string in each elements.And "this.hasnext()" will return true its when iteration has more values,which means that if there still has words in your current object array, it will continue to display the names of the person to the screen. Else, it will get out the block of "if" and return to null.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.