Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

usage of this.

 
Patrick de Kruijf
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Patrick de Kruijf
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patrick de Kruijf wrote: I will pay more attention to my question in the future.

Good. you are welcome
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Patrick de Kruijf
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch......
I assure you one thing You will love this community..
 
Patrick de Kruijf
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for this 'AHA' moment!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Patrick de Kruijf wrote:Thank you very much for this 'AHA' moment!


What's that?
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11888
203
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh!, is this that? just came to know that! Thanks Andrew Monkhouse!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic