Results 1 to 8 of 8
Thread: Why is "this" used there?
- 06-15-2011, 02:26 AM #1
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Why is "this" used there?
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.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(); } }
Thanks!
- 06-15-2011, 02:49 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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; } }
- 06-15-2011, 02:54 AM #3
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
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?
- 06-15-2011, 02:56 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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).
- 06-15-2011, 02:58 AM #5
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Okay thanks. I appreciate it. Any other books that you'd recommend?
- 06-15-2011, 02:58 AM #6
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Oh by the way, where did you get these small projects? Are those things that you made up for yourself?
- 06-15-2011, 03:06 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 06-15-2011, 03:36 AM #8
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Similar Threads
-
Setting up "Stop" and "Resume" with SourceDataLine
By davisburns in forum Advanced JavaReplies: 0Last Post: 03-08-2011, 03:05 PM -
connection = DriverManager.getConnection(DATABASE_URL,'"+userid +"','"+password+"');
By renu in forum New To JavaReplies: 3Last Post: 10-12-2010, 04:21 PM -
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks