Jump to content

Confusion about the "this" keyword and contructors vs instance variables.

- - - - -

  • Please log in to reply
4 replies to this topic

#1
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
So I'm totally confused. I know that there is a local version of the variable in the method and and so we have to use this to use it in another class. But then why would we make a method with parameter that are different like in the main class, you're taking in two variable in a (for example) constructor(int a, int b);
And then when your process that inside, you re-assign them to different variables like int point1 = a; point2 = b;
Why would you do that instead of using the "this".

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Can you provide an example of what you mean? The constructor's arguments (int a, int b) only exist within the constructor's scope, so you would normally assign them to this->point1 or this->point2 to preserve them in the class if that is what you wished to do, otherwise they disappear.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Simonxz

Simonxz

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
Let's take for example a class called Banana


public class Banana {

    String color;

    String taste;

    boolean rotten;

    int calories;

   

    public Banana(String color, String taste, boolean rotten, int calories) {

        this.color = color;

        this.taste = taste;

        this.rotten = rotten;

        this.calories = calories;

    }  

}


When we use the keyword "this" it means this instance.

When we call the constructor and give it the parameters, color, taste, rotten, calories. Those variables only exist in the scope of the constructor, we want them in the scope of the class.

In the line :
this.color = color
We tell the compiler, I want the variable color of my class to equal the variable color of this scope. You can bypass the this keyword by using a different variable name in your constructor header. So let's say, if I were to have this constructor.


public Banana(String myColor) {

    color = myColor;

}


It would be valid.

You can also use the this keyword to call a constructor in your class. It's just a little way of saving yourself some time by not writing the same code twice.

public class Banana {

    String color;

    String taste;

    boolean rotten;

    int calories;

   

    public Banana(String color, String taste, boolean rotten, int calories) {

        this.color = color;

        this.taste = taste;

        this.rotten = rotten;

        this.calories = calories;

    }  

    

    public Banana() {

        this("yellow", "good", false, 100);

    }

}


But make sure you call a constructor that exists in your class or you'll get an error.

So if you were to call

Banana b = new Banana();

Somewhere in your code.
b would be yellow, good, not rotten and have 100 calories.

But if you were to call

Banana b = new Banana("brownish", "bad", true, 120);


You'd have a brownish, bad taste, rotten with 120 calories banana.

#4
An Alien

An Alien

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 260 posts
Thanks a lot! I love the banana example and I love bananas too, lol :c-biggrin:

One more question, the instance variable in your example is "b", right?

#5
Simonxz

Simonxz

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
Yes, in my example, b is an instance of Banana




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users