• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

usage of this.

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Good. you are welcome
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Patrick de Kruijf
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for this 'AHA' moment!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



What's that?
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh!, is this that? just came to know that! Thanks Andrew Monkhouse!
 
It runs on an internal combustion engine. This ad does not:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic