Results 1 to 15 of 15
Thread: "this" keyword
- 07-06-2011, 02:26 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
- 07-06-2011, 02:47 PM #2
This is a program you are developing for an Android phone?
The operator "this" uses the object's own reference as the constructor for the TextView class. This isn't a compilable and runnable program, so I can't really expand on how its used there.- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 07-06-2011, 03:14 PM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
If a method takes an argument of the current class you are working in you can pass a reference as "this", this refers to the object you are currently working in.
Let's say you have a class, X, which has in it an object of type Y, object Y's constructor takes an object of x, you can pass this into Y like this
There are flaws in the below snippet, it's really meant as a basic example to show the idea.
This uses this in two ways, one to pass an argument, the other to differentiate variables(this.x = x;).Java Code:public class X{ private Y y; public X(){} public void createY(){ y = new Y(this); } } public class Y{ private X x; public Y(X x){ this.x = x; } }
- 07-06-2011, 03:35 PM #4
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
ok ..~ thanks for the help..~
is this some sort of shortcut? why aren't they jotted down in the java documentation?
my friend said that it was a context reference. is it true?
I have encountered this for several times but didn't know the usage of it and always forget to seek for the answer..~
I'm just begin to learn how to write android..~ so that i can add a few more lines on my future resume..~ haha
- 07-06-2011, 03:36 PM #5
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
ok ..~ thanks for the help..~
is this some sort of shortcut? why aren't they jotted down in the java documentation?
my friend said that it was a context reference. is it true?
I have encountered this for several times but didn't know the usage of it and always forget to seek for the answer..~
I'm just begin to learn how to write android..~ so that i can add a few more lines on my future resume..~ haha
- 07-06-2011, 03:38 PM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
- 07-06-2011, 03:41 PM #7
It is documented, but probably not so much in Android documentation. From what I've seen in Android documentation it expects you to already understand at least the basics of Java.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 07-06-2011, 04:22 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
As with all things Java you ought to learn the basics of Java before trying to code in Swing, JDBC, any number of frameworks, or Android.
- 07-06-2011, 06:54 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
I do have java basic, just I seldom encounter passing "this" as an argument. And I took java during my second semester in my university level.
The code (android) was just happened to be chosen by me accidently.~ It did not matter what kind of code from which field that I were going to choose, because I just want to know how it works.
Anyway, thanks for the helps and i really appreciate..~ hahahhahahaha
- 07-06-2011, 06:56 PM #10
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
This isn't the program, just the code for me to learn how to code Android..~
- 07-06-2011, 06:58 PM #11
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
I do know on how to use "this" with a field and constructor, but it didn't mention it can be used as an argument..~
- 07-06-2011, 08:00 PM #12
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.
The tutorial does not show an example, however this part of the text explains what it does. Anything that can accept the reference of the object that this refers to can take this as a valid argument. I think the examples show alternative ways to how you can use it, more common ways too.- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 07-07-2011, 09:26 AM #13
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
- 07-08-2011, 08:02 AM #14
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
As others have said "this" is good to use as a variable including as an argument provided it is of the right type.
In Android terms, the TextView constructor wants an instance of Context in its constructor. (so that the text view knows its context...) The code you posted is typical of an Activity or subclass - it says what should happen when the activity is first started or when the user navigates back to it. Now since an Activity IS-A Context "this" (the activity instance) is just the thing to pass to the TextView constructor.
- 07-08-2011, 08:10 AM #15
Similar Threads
-
How to use "this" Keyword
By Gousia in forum New To JavaReplies: 3Last Post: 03-01-2011, 01:50 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


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks