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".
Confusion about the "this" keyword and contructors vs instance variables.
Started by An Alien, Mar 12 2011 04:50 PM
4 replies to this topic
#1
Posted 12 March 2011 - 04:50 PM
|
|
|
#2
Posted 12 March 2011 - 06:02 PM
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.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#3
Posted 12 March 2011 - 06:11 PM
Let's take for example a class called Banana
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 :
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.
But make sure you call a constructor that exists in your class or you'll get an error.
So if you were to call
b would be yellow, good, not rotten and have 100 calories.
But if you were to call
You'd have a brownish, bad taste, rotten with 120 calories 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 = colorWe 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
Posted 12 March 2011 - 08:57 PM
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?
One more question, the instance variable in your example is "b", right?
#5
Posted 12 March 2011 - 09:31 PM
Yes, in my example, b is an instance of Banana
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









