Results 1 to 16 of 16
Thread: The this keyword
- 12-12-2012, 05:29 PM #1
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
- 12-12-2012, 05:52 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: The this keyword
It's the object you're in.
Please do not ask for code as refusal often offends.
- 12-12-2012, 06:49 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
Re: The this keyword
I like the way SmallTalk handles those problems: something (an object) talks to another object. What the object has to say is the method. The destination of the method can be the object itself. So the first object talks to itself; SmallTalk manages this (sic) with the 'self' keyword. C++ and Java (and some more languages) use the word 'this' instead.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-12-2012, 07:55 PM #4
Re: The this keyword
What these guys said.
It's easy to forget that at runtime, the code you are looking at gets instantiated into an object, and that might might have many objects of the same type created from the same blueprint (the code). When you say 'this' in one of those objects, it specifically means the instance executing the code in question, not another instance. Here is a simple example:
In this example, the keyword 'this' allows us to distinguish between the variable called 'name' that belongs to the Person class, and the variable called name that is a parameter in the Person constructor. this.name = name; means in english: "Take the variable called name belonging to me (this instance of person) and assign it the value of the parameter called name that was supplied to me in my constructor".Java Code:public class Person{ String name; public Person(String name){ this.name = name; } }
Another example might be something like this:
Java Code:public class Alpha{ public int value; public Alpha(int initialValue){ value = initialValue; // I didn't use this here because the names are distinct, but I could have if I wanted to! } public void alphaPrinter(Alpha alpha){ System.out.println(alpha.value); } public void test(){ alphaPrinter(this); //prints the value of whatever 'this' alpha contains alphaPrinter(new Alpha(3)); //prints a 3 } } //... public class Main{ public static void main(String[] args){ Alpha a = new Alpha(25); a.test(); //this will print a 25, then a 3 because inside 'a', it's own value is printed first, and then a new alpha is created and printed. } }
- 12-12-2012, 08:08 PM #5
- 12-12-2012, 09:45 PM #6
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: The this keyword
I still don't understand...
Are you saying it's like a parent gives a kid a name but then that kid has another kid and that kid gives his kid his name so then they all share names?
- 12-12-2012, 11:28 PM #7
Member
- Join Date
- Aug 2012
- Location
- Switzerland
- Posts
- 49
- Rep Power
- 0
Re: The this keyword
it means that THIS x, the one in the parenthesis of the method would be equal to the variable x, so if in another method I writeJava Code:int x; public setX(int x){ this.x = x; }
setX(9);
it will say: this x, or the int in the parenthesis, the argument, is equal to the variable x, so now x=9
- 12-13-2012, 08:36 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,604
- Blog Entries
- 7
- Rep Power
- 17
Re: The this keyword
No, simply think of 'this' as 'I' or 'my' or 'mine'; e.g. 'this.name= name' reads as 'my name gets the value of name' and 'this.doSomething()' reads as 'I doSomething()'. etc. etc. It's as if each object has a mind of its own and it knows about itself by the keyword 'this'. So for each object 'this' means something else ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-13-2012, 08:38 PM #9
Senior Member
- Join Date
- Jan 2012
- Posts
- 146
- Rep Power
- 0
Re: The this keyword
See and that is where I get confused. Doesn't the object "already know" something about itself. I get confused because I think this is pretty useless.
- 12-13-2012, 08:46 PM #10
Member
- Join Date
- Aug 2012
- Location
- Switzerland
- Posts
- 49
- Rep Power
- 0
Re: The this keyword
simply, this refers to the object that is in the arguments of the method, so you can use the value of that and assign it to a variable for example
- 12-13-2012, 09:02 PM #11
Member
- Join Date
- Dec 2011
- Posts
- 25
- Rep Power
- 0
Re: The this keyword
What about a situation like this?
You have a variable in the class named 'a' and an argument in the method test named 'a'?Java Code:public class Example { public static void main(String[] args) { Example example = new Example(); example.test(10); } public int a = 5; public void test(int a) { // What is the value of a? 10 or 5?? } }
How do you say that you want to access the class variable 'a'?
The solution is to use this.a
Look at the output of that programJava Code:public class Example { public static void main(String[] args) { Example example = new Example(); example.test(10); } public int a = 5; public void test(int a) { // What is the value of a? 10 or 5?? System.out.println("'this.a' is equal to " + this.a + "!"); System.out.println("However, 'a' is equal to " + a + "."); } }
- 12-13-2012, 09:53 PM #12
Re: The this keyword
Only because you don't understand it. It is a very common idiom to have a class that implements an interface use a reference to itself. An example is an action listener. If you had something likeI think this is pretty useless.
Another example would be something like this:Java Code:public class Window extends JFrame implements MouseMotionListener{ public static void main(String[] args){ new Window(); } public Window(){ //Without 'this', how would I do this? This is a pretty common technique. addMouseMotionListener(this); } public void mouseDragged(MouseEvent e){ //do something on a mouse click'n'drag } public void mouseMoved(MouseEvent e){ //Do something on a mouse move } }
Java Code:public class Alpha{ private Alpha parent; public void setParent(Alpha parent){ this.parent = parent; } //In this case, the current alpha instance is the parent. It creates inside of it //another instance of alpha called child. To tell the child who it's parent is, //we pass it a reference to the current object. Can you think of a way to //do this without 'this'? public void introduce(Alpha child){ child.setParent(this); } public static void main(String[] args){ Alpha parent = new Alpha(); Alpha child = new Alpha(); parent.introduce(child); } }Last edited by quad64bit; 12-14-2012 at 04:05 AM.
- 12-13-2012, 09:56 PM #13
Re: The this keyword
P.S. 'this' is used a LOT in data structures like linked lists, trees, maps, graphs, etc...
- 12-14-2012, 03:01 AM #14
- 12-14-2012, 03:40 AM #15
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 697
- Rep Power
- 6
Re: The this keyword
Yes, the object know it self. But think again in this example:
See how we use this keyword in the constructor. In the constructor we have an argument named name. To assign name argument to the User's name field you have to use the this keyword. Because if you simply use name = name; then it will not get you anywhere. Because the name argument is only available in the constructor. That's why when you want to refer to User's name field you have to use this.name.Java Code:public class User { private String name; public User(String name) { this.name = name; } }
If the name of your class field in not the same with the constructor argument then you can simply use the field name without the this keyword. For example:
I hope this example using the constructor can help you to understand it better.Java Code:public class User { private String name; public User(String username) { name = username; } }Website: Learn Java by Examples
- 12-14-2012, 04:01 AM #16
Similar Threads
-
Using 'this' keyword in constructor
By kudwn in forum New To JavaReplies: 2Last Post: 10-11-2010, 12:12 PM -
Using 'this' keyword in constructor
By kudwn in forum New To JavaReplies: 5Last Post: 10-11-2010, 09:47 AM -
Use of keyword instanceof
By darek9576 in forum New To JavaReplies: 3Last Post: 03-14-2010, 10:35 PM -
this keyword
By coltragon in forum New To JavaReplies: 10Last Post: 03-01-2010, 09:20 AM -
Use of this keyword
By Java Tip in forum Java TipReplies: 0Last Post: 11-18-2007, 07:32 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks