Results 1 to 8 of 8
  1. #1
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    5

    Default Why is "this" used there?

    Java Code:
    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() {
            //print out values of even indices of the array
            InnerEvenIterator iterator = [COLOR="red"]this.[/COLOR]new InnerEvenIterator();
            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);
            }
            
            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();
        }
    }
    I don't get why you would use it there, since it is all within the same class and it's the only time this constructor is evoked to create this object.

    Thanks!

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It's really just there to clarify the code. Some may argue that it would be clear without the this, but some people like using this for clarity.

    For example, in this constructor, the this is also not necessary, but some people use it anyway.

    Java Code:
    public class X{
      private int x;
      public X(int y){
        this.x = y;
      }
    }

  3. #3
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    5

    Default

    Oh okay. I wasn't sure how necessary it was, so your point clarifies it. It is not necessary but it makes reading the code a little bit easier.

    Thanks!

    I am fairly new at this, so just to keep hope alive. how long have you been programming and are you self taught or did you learn in school?

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'm self taught at the moment, but heading to college in a few months. I have been reading books on java for about 6 1/2 months now and previously read How to Design Programs. About 7 months total time spent learning. There is still a lot I do not know and I am still somewhat of a beginner. It takes some time to get through the basics, but it will come eventually. Read a few good books and do some practice projects(something small enough that it's possible, but challenging enough that you will have to think about it).

  5. #5
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    5

    Default

    Okay thanks. I appreciate it. Any other books that you'd recommend?

  6. #6
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    5

    Default

    Oh by the way, where did you get these small projects? Are those things that you made up for yourself?

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Helping beginners get started. Sunde's Blog

    That's a link I usually link people to with some books I like to recommend.

    For the small projects you usually want to do something that interests you. I unfortunately don't have a great imagination, some things I have done are, a GUI calculator like the one on windows or any other operating system, a simple notepad, a more advanced text editor, and black jack.

    Make sure to also type out examples in books, it's definitely helpful, and do the exercises. As of late I've been reading without a computer by my side so i don't get as much practical experience as I'd like.

  8. #8
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    5

Similar Threads

  1. Setting up "Stop" and "Resume" with SourceDataLine
    By davisburns in forum Advanced Java
    Replies: 0
    Last Post: 03-08-2011, 04:05 PM
  2. Replies: 3
    Last Post: 10-12-2010, 05:21 PM
  3. Replies: 1
    Last Post: 01-21-2010, 10:20 AM
  4. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Tags for this Thread

Posting Permissions

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