Results 1 to 13 of 13
Thread: the word "this"
- 02-25-2010, 09:10 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 31
- Rep Power
- 0
the word "this"
I see the term, "this" used here and there and I don't understand what it means. I have searched around and has been said it means the current item. Well, I understand current item when using a cursor traversing through a linked list and how it stops at the current item. maybe that's it? Even if I'm hitting the nail on the head here, I don't understand how "this" is used. I have seen "this" used in two ways.
one way was something like this: this.xyz = xyz;
another way I am seeing it in my book in the binary trees chapter is this:
{
right = right.removeRightmost( );
return this;
}Last edited by vendetta; 02-25-2010 at 09:15 PM.
- 02-25-2010, 09:16 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Do you understand it if you substitute 'my' (or 'me') for 'this'?
kind regards,
Jos
- 02-25-2010, 09:31 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 31
- Rep Power
- 0
so far I understand "my" like this:
say I have a class called "AClass", and I have a separate driver file called "MyDriver":
AClass MyDriver = new aClass();
and I'm using the test file like this
MyDriver.add();
MyDriver.addMoreStuff();
MyDriver is the example of "my" that I know of.
- 02-25-2010, 10:47 PM #4
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
This seems familar... Check out this thread to see if it helps
this keyword
as for your second example
In this case, the keyword this would mean the current object(class instance) being worked in.Java Code:{ right = right.removeRightmost( ); return this; }
- 02-25-2010, 11:11 PM #5
Member
- Join Date
- Jan 2010
- Posts
- 31
- Rep Power
- 0
- 02-25-2010, 11:37 PM #6
Member
- Join Date
- Jan 2010
- Posts
- 31
- Rep Power
- 0
what would be a current item in this program?
can the "this" keyword be substituted anywhere in here?
Java Code:public class MyDriver { public static void main(String[] args) { AClass MyDriver = new AClass(6,5); MyDriver.addItem(5); MyDriver.addItem(10); MyDriver.addItem(4); MyDriver.printItems(); } }
the output of the program above is:Java Code:public class AClass { int a,b; private int[] data = new int [100]; int index = 0; public AClass(int inA, int inB){ a = inA; b = inB; } public void addItem(int n){ data[index]=n; index++; } public void printItems(){ for (int i=0; i<index; i++){ System.out.println(data[i]); } } }
5
10
4
- 02-25-2010, 11:55 PM #7
Senior Member
- Join Date
- Feb 2010
- Posts
- 128
- Rep Power
- 0
From what I have been told at uni is that "this" determines between the variables. Let me explain:
As you can see my variables are named the same as variables in my Course constructor. Now this is weird cause we all know that you cannot declare a variable with the same datatype or name twice. So when I say "this", i mean "Variable in THIS CLASS.<variable name> = this local variable received by the constructor.Java Code:public class Course { private String courseName, courseId; private Module [] modules; public Course(String courseName, String courseId, Module [] modules) { this.courseId=courseId; this.courseName=courseName; this.modules=modules; } }Measuring programming progress by lines of code is like measuring aircraft building progress by weight.
- 02-26-2010, 03:36 AM #8
Member
- Join Date
- Nov 2009
- Posts
- 41
- Rep Power
- 0
"this" can only be used in instance methods or constructors.
Instance methods are basically methods without "static" in them.
Constructors are methods which create an object(instance) of a class.
So in your class the constructor is:
So when the constructor is called with following code:Java Code:public AClass(int inA, int inB){ a = inA; b = inB; }
It creates a variable "MyDriver" of the type AClass which consists of:Java Code:AClass MyDriver = new AClass(6,5);
int a = 6
int b = 5
an integer array of length 100
int index = 0
Such a variable is called "object" or "instance of a class"(in your case it's an instance/object of AClass).
And there is a nice Sun explanation of what "this" is used for. You can find it here:
Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
- 02-28-2010, 06:58 AM #9
Member
- Join Date
- Dec 2008
- Posts
- 41
- Rep Power
- 0
"this" can also be used in place of a body of a constructor if you have one fully qualified constructor.
- 02-28-2010, 08:08 PM #10
Member
- Join Date
- Jan 2010
- Posts
- 31
- Rep Power
- 0
here is what I learned from the sun link:
the comparison makes sense, sounds like it is an option either way.Java Code:public class Point { public int x = 0; public int y = 0; //constructor public Point(int a, int b) { x = a; y = b; } } but it could have been written like this: public class Point { public int x = 0; public int y = 0; //constructor public Point(int x, int y) { this.x = x; this.y = y; } }
any thoughts on what "return this" could be here?
Java Code:public BTNode<E> removeLeftmost( ) { if (left == null) return right; else { left = left.removeLeftmost( ); return this; } }
- 02-28-2010, 08:59 PM #11
it seens you understand the use of "this" for constructors where refers to the local varialbe of the constructor that tacks the value of the variable you use it when creating a new object
that return statement returns the courent values or position of the object
StormyWaters allready explained that to you :)
well look at your function type it needs to return an object, more precisely the courent object that is been used so this is what
doesJava Code:return this;
silence i'm trying to meditate:p
- 02-28-2010, 09:39 PM #12
Member
- Join Date
- Jan 2010
- Posts
- 31
- Rep Power
- 0
what is a current item? in a method there are several values changing. is a current item the last one that was changed? is it the line above "return this"?
is there a current item in the method below? or does a method need to return something to have something labeled as current?
Java Code:public void method1(int first){ int number; number = first; }
- 03-01-2010, 08:33 AM #13
no is not like that
the method you posted there is declared as void so it can not return something is just a pice of cod that will be executed evrytime you will call it
that preview method of yours well that one require something to return and it needs to be the same type as the metho type wich meant an object namely the current object which is used
an object is created when you call the constructor method let's say you have a class :i tryed to make it as simple as possible to show what is an objectJava Code:class MyClass { int var1; int var2; MyClass(int var1, int var2){ //this is the constructor this.var1 = var1; //in this case this is used to indicate which var1 is refered too this.var2 = var2; } public print() { System.out.println("you have created an object with: "+ var1+","+var2); } //now in main method we will creat an object of this class by useing the constructor method public static void main(String args[]) { MyClass obj = new MyClass(5,6); //this is the part when you creat an object obj.print(); //here is how to use his method } }
you cannot return an object by returning his variables values becous they are not an object type so when you need to return an object either way you userefering to the courent object created or if you have more then 1 objects created you can return the object nameJava Code:return this;
my advice is to read carefully what is object oriented programming and understanding the concepts behind themsilence i'm trying to meditate:p
Similar Threads
-
Reserved word "throws"
By Lil_Aziz1 in forum New To JavaReplies: 5Last Post: 01-02-2010, 02:12 PM -
Count lines cointaining "word" in input file
By gwithey in forum New To JavaReplies: 5Last Post: 04-02-2009, 05:23 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 -
replacing last comma in a string with the word "or"
By wprjr in forum New To JavaReplies: 1Last Post: 05-07-2008, 01:19 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks