aspose file tools*
The moose likes Beginning Java and the fly likes usage of this. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "usage of this." Watch "usage of this." New topic
Author

usage of this.

Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
Hello,

I'm staring at a code I copied from a reader I got from my school, I understand most of it but I can't figure out what the usage is of this. .

What does this. do?



Thanks in advance for your time and effort!


------------------8<------------------
please cut here
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575



just
1.use constructor to set values and get the values back using getters
2. now remove the this prefix attachment in constructor and follow the setp 1.

both the results are same?

typically, this represents currently executing object.

As a side note: please follow the method convention of JavaBean. and your constructor missing his name

hth
Vishal Kashyap
Ranch Hand

Joined: Aug 07, 2010
Posts: 73

Patrick de Kruijf wrote:Hello,

I'm staring at a code I copied from a reader I got from my school, I understand most of it but I can't figure out what the usage is of this. .

What does this. do?



Thanks in advance for your time and effort!


First of all,
your constructor have class name which is missing in your code written here.

and as
Seetharaman Wrote,
typically, this represents currently executing object.


the object you are working on is is the current object for you. means, suppose you have written



then s1 is your current executing object.
and on executing constructor for value initialization, argument values get assigned to current executing object's Instance Variable's value i.e. s1.name,s1.amount and s1.value. And in the above code these values may also be written as this.name,this.amount and this.value.




MCSA 2003 | Preparing For OCPJP/SCJP6
Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
I see now I have forgotten the classname, I was posting this code because of this. and didn't pay much attention to the rest of the code. I realise that these mistakes can be very confusing when answering my question. I will pay more attention to my question in the future.

You say I have to follow the JavaBean convention on methods, you mean I had to type?

instead of

Thanks again!
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Patrick de Kruijf wrote: I will pay more attention to my question in the future.

Good. you are welcome
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Patrick de Kruijf wrote:
You say I have to follow the JavaBean convention on methods, you mean I had to type?

instead of

Yea, It'll be helpful in your future Java life!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36513
    
  16
Welcome to the Ranch
Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
Thanks for your replies and the warm welcome to The Ranch, hope to learn a lot from you, and uh Howdy!
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Welcome to JavaRanch......
I assure you one thing You will love this community..


SCJP6.0,My blog Ranchers from Delhi
Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
I have started writing the code below for a assingment for my school, I think I almost understand what the use of this.is.

Is it true that this.age refers to the parameter put in the setAge(int age) method in the class Goose?

Am I right when I say that this.age refers to the argument send from gilbert.setAge in the class GooseTestDrive? (if the Object made from the Goose class is Gilbert ofcourse.)



Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Patrick de Kruijf wrote:I have started writing the code below for a assingment for my school, I think I almost understand what the use of this.is.

Is it true that this.age refers to the parameter put in the setAge(int age) method in the class Goose?

Am I right when I say that this.age refers to the argument send from gilbert.setAge in the class GooseTestDrive? (if the Object made from the Goose class is Gilbert ofcourse.)


In your GooseTestDrive class, you've created gilbert as instance of Goose, and setting the age and weight. And, this.age in your Goose class' means, the instance variable of the object on which you are executing the method, like setAge(). for example, the objet is gilbert
Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
So when I write gilbert.setAge(4); in the GooseTestDrive class, then this.age in the Goose class is 4 for the object gilbert of class Goose.

And then when I say this.age = age in the class Goose; The instance variable age gets a value of 4, but only for the object gilbert of the class Goose?
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Patrick de Kruijf wrote:So when I write gilbert.setAge(4); in the GooseTestDrive class, then this.age in the Goose class is 4 for the object gilbert of class Goose.

And then when I say this.age = age in the class Goose; The instance variable age gets a value of 4, but only for the object gilbert of the class Goose?


Correct, for gilbert, it'll set as 4, and if you create another object of the class Goose, and set different number, then for this object the age will be different(the one you've set).
Patrick de Kruijf
Ranch Hand

Joined: Mar 02, 2010
Posts: 63
Thank you very much for this 'AHA' moment!
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Patrick de Kruijf wrote:Thank you very much for this 'AHA' moment!


What's that?
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11279
    
  59

Abimaran Kugathasan wrote:
Patrick de Kruijf wrote:Thank you very much for this 'AHA' moment!


What's that?

According to Dictionary.com it is "an instant at which the solution to a problem becomes clear". It is a well studied phenomenon with scientists studying and explaining it.

I've been told by some teacher friends that the sign of a really great teacher is one who watches their students as they are explaining a concept, and they keep trying to explain the same concept in different ways until the student gets the expression on their face that indicates they've just had their "aha" moment.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Oh!, is this that? just came to know that! Thanks Andrew Monkhouse!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: usage of this.
 
Similar Threads
Using 'volatile'
EJB unable to find my Home interface
Set that only Items can be added
struts 2: Stuck up with simple issue
Value objects and encapsulation