Results 1 to 4 of 4
  1. #1
    paty is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

    Default what does "this" mean?

    I know that is a silly question but I am looking for a definitive answer. I have used it but I can't explain it.

  2. #2
    Swamipsn is offline Member
    Join Date
    Jul 2007
    Posts
    74
    Rep Power
    0

    Default

    Hi,
    "this" means you indicates the variable or instance which is used by this[Present] class. Suppose your base class have int x, and derived class also have int x then you need to use this key word to point out the derived class's
    x. .....

  3. #3
    yiweiang is offline Member
    Join Date
    Aug 2007
    Posts
    15
    Rep Power
    0

    Default

    Hi there, let me give you an example of "this" that will help you grasp this concept better. Assume you have a class, called Dog. The Dog has three variables, age, height and weight. And you have a constructor:

    public class Dog
    {
    int age;
    int height;
    int weight;


    public Dog(int age, int height, int weight)
    //Constructor. The calling method will parse int age, height and weight
    {
    /*
    So this is the problem. both variables have the same name! age that is passed in and age that is the class variable! so to solve it, you use "this"*/

    this.age=age;
    this.height=height;
    this.weight=weight;
    }
    }

    So in the three lines of "this.age" "this.height" and "this.weight". it sets the CLASS variable, the one declared in the lines right after the class declaration to the three integers that are passed in.

  4. #4
    paty is offline Member
    Join Date
    Jul 2007
    Posts
    41
    Rep Power
    0

    Default

    thanks guys
    yiweiang you were very clear
    It was driving me crazy

Similar Threads

  1. Hwlp with "Open", "Save", "Save as..."
    By trill in forum New To Java
    Replies: 3
    Last Post: 11-02-2010, 09:26 AM
  2. Replies: 1
    Last Post: 03-14-2008, 02:51 PM
  3. Replies: 1
    Last Post: 07-27-2007, 10:59 PM
  4. Replies: 1
    Last Post: 07-25-2007, 10:55 PM
  5. Replies: 1
    Last Post: 07-16-2007, 06:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •