• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

The "This" keyword?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can someone in plain english please define the this keyword..my book explains it but i still have trouble understanding the meaning..yes im a n00b.. lol
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you looked at Sun's Tutorial about the keyword?

http://java.sun.com/docs/books/tutorial/java/javaOO/methodbody.html


From what I understand it saying....
You can name a variable the same thing as you name an argument into a method. when you are referring to that variable...how do you tell the computer which variable you are referring to - the argument or the variable you declared?

They use the 'this' keyword to refer to the variable that refers to the argument in the method and they use the 'super' keyword when they are referring to the variable in the class that you defined.

I'm still learning java myself - so there may be something else I don't know about it - or that I didn't understand when I read it But maybe that helps ya...
 
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The this keyword is used to reference the class you are in; for instance you have

Class A{

public A getA()
{
return this;
}
}

This is practically saying return a reference of A to the caller of this method.

You can also use it to call methods or variables of A i.e. this.getA(); it does not mean that you could not have written getA(); but some would prefer this.getA() for reasons best known to them; you can use it in your constructor to refer to another cnstructor or use it to pass a given class as a parameter to another method. I think there are were it actually comes in handy.
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I will try.


Every method and variable is defined in a class. We can't just use the methods or variables we must first have a reference to the object which we create. ie Person me = new Person(). In my example me is the reference. But in my class definition below I have no idea (that is no idea when I defined the class) what name I will use to define a reference ahead of time, and in fact I can define several different reference variables. ie Person your = new Person(); Person he = new Person();... But as I said in the class definition we wouldn't know what the reference variables are but we need some reference to use the makeNoise() method from inside the fart() method, because as I mentioned when ever we use a method or a variable we must have a reference to a object in which it is defined. So some smart java developers deceided what if we use the key word this to take the place of the references which will be declaired after the class is defined. So in Person me = new Person() the 'this' reference inside of 'me' will refer to 'me' when it is created (self reference), and for a different reference Person you = new Person() 'you' will mean the same thing as 'this' for the object 'you' referes to. So when you.fart() the fart() method can call the makeNoise() method because 'this' in the 'you' object created, will refer to itself.

The thing to remember is that (in this example) you don't actually need to write this.makeNoise(). You can but if you don't it will be done behind the scenes somewhere. So to summarize 'this' is a self reference which doesn't need to be defined unless you have some naming conflicts and then you should actually write the this before your methods and variables.

 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anselm, are you sure that "this" returns a reference to the current class? i was under the impression that it returned a reference to the current instance of a class, at least in most cases. anyway, that's how i've been using it, and my code seems to work so far...
 
Anselm Paulinus
Ranch Hand
Posts: 390
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by M Beck:
Anselm, are you sure that "this" returns a reference to the current class? i was under the impression that it returned a reference to the current instance of a class, at least in most cases. anyway, that's how i've been using it, and my code seems to work so far...




Well; you probably right. It probably returns a reference to the instance of the class. Basically I was only trying to convey the use of this as simple as I could.
 
You showed up just in time for the waffles! And this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic