#How does one use this() in this example?
1 messages · Page 1 of 1 (latest)
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
I was given this example, I'm learning OOP as a beginner. What exactly does "this" do here?
first: This(h) should be this(h)
and for your question:
you know what constructors are?
I just learned today what they are
ok so you have two constructors in that example:
public class Rectangle {
double largeur = 3.0;
double hauteur = 4.0;
public Rectangle(double h) {
hauteur = h;
}
public Rectangle(double h, double l) {
this(h);
larguer = l;
}
}
so basically the this keyword just refers to the current object this keyword is used in
so in this example an instance of Rectangle
of the second "Rectangle" right?
you mean the second constructor of Rectangle?
yes
yeah so in this example this(h) just calls the other constructor in this class which takes in only one double value
no
but this can also be used differently
"this(h);" refers to this line "public Rectangle(double h, double l)" ?
this is the current instance no?
Ok I don't get it then
Why is is calling the other constructor?
if it is the current instance?
to avoid duplicated code, its still the same instance
imagine your Rectangle or other class got way more fields that get set in the constructor and some of these might be the same for every constructor
@tribal sun If I had 3 constructors for example, and I used "this" in one of them, how would it know which of the other two to use?
they need to be different, this means they need to have different parameter
oh, so you're saying it relates to the first constructor becuase it only has one parameter???
yeah so this(h) calls the first because first of all h is a double and there is one constructor which accepts a double
well both of them use doubles, but only of them has one paremeter with one double
yeah exactly there is only one constructor which only takes one double
ok, so if I said this(x) and this(x,y) within the same cosntructor, those two would refer to two different constructors, right?
link?
basically this explains it
From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation.
This beginner Java tutorial describes fundamentals of programming in the Java programming language
I have two other questions btw
go on
Ok, so why do we do this exatly? I assume it's because we don'T want redudancy by having to copy paste the whole code?
this shows it:
instead of always calling these four lines you could use the already existing constructor and give them your values
without this key it would looks smth like this:
public Rectangle() {
this.x = 0;
this.y = 0;
this.width = 1;
this.height = 1;
}
public Rectangle(int width, int height) {
this.x = 0;
this.y = 0;
this.width = width;
this.height = height;
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
those four variables refer to the global variables inside the class right?
yeah as said this can also be used to distinct local variables with fields
im going to read yout link tomorrow, going to go sleep that'S why I don't have time
oof hard times
can you reformulate this sentence?
yeah
you mean "this" can be used as an instance of a current constructor?
take this as an example
you might want to name a parameter the same as an already existing field.
here in the example there are x,y as fields set to 0.
you now have a constructor which also got parameter x and y and your program (compiler) needs
to know which x you mean and you can tell him via the this key
public class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int x, int y) {
x = x;
y = y;
// after this x and y are still 0 no matter of the parameters value
}
}
public class Point {
public int x = 0;
public int y = 0;
//constructor
public Point(int x, int y) {
this.x = x;
this.y = y;
// after this x and y (fields) are set to the values passed in the parameter
}
}
The most common reason for using the this keyword is because a field is shadowed by a method or constructor parameter.
yes this one i actually understood before
This sentence I still dont get
I mean that with that sentence
oh ok
so yes, when you don't use this(), but only with variables like this.x, it refers to the current constructor, am I right?
I have one last question after and Im finished
also known as members of a class
example:
public class Rectangle {
// fields
private int x, y;
private int width, height;
public Rectangle() {
this(0, 0, 1, 1);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
...
}
ah ok, so those are teh varaibles of the class
yeah
ok, so i assume you agree with what i said here
yeah
ok
When you don't refer to another constructor with this(), but you'Re using this.x=whatever, it means you are refering to the current object of the current constructor within which you are inside
when you don't use this(), but only with variables like this.x, it refers to the current constructor, am I right?
this(...)refers to a constructor of the current class
this.nameOfFieldrefers to a field of the current class namednameOfField
yeah
this always refers to the current class
also works with method calls
but it is always another constructor of the one within which you are using
as we said before
you mean the this(...) call?
yes
yeah
ok good
now my last question is
In this line for example
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
you'Re calling the third constructor, right?
yeah, the one that accepts 4 integers
ok, and when it calls this constructor, what happens afterward?
what is it doing exactly?
it just created the object like a normal constructor
an object with 4 values?
yeah an instance of Rectangle and if you just call the ... = new Rectangle(); it will set the x,y,width,height fields to 0,0,1,1 as default values
the values are based on the constructor and parameters if they exist
but in this line public Rectangle(int width, int height) {
this(0, 0, width, height);
}, we know the first two values are zeros, the width and the height will come from where? those which are passed as parameters of those which come from the third constructor?
it'S what is passed as parameteres I assume right?
yeah
so if you create an instance like this:
Rectangle instance = new Rectangle(10, 10);
it will go into this constructor:
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
and then it goes into this constructor using the this keyword:
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
so the values are like:
x = 0
y = 0
width = 10
height = 10
because the user of that class which created an instance gave 10, 10 as parameters
Yes,, this makes sense.
so to return to my original example
here, the second constructor calls the first one, and assigns hauteur = h as a property of the second constructor
yeah
so, my object at the end will have the following hauteur = h and largeur = l
if you call the second constructor the user will give information about the value of h and l and it will pass the information of h to the first constructor and calls it
yeah
it will pass the information as a parameter to the first constructor
yeah
np
OOP can be very confusing
i learned all fo this today, and my teacher genuinely is bad
so i have to guess sometimes
yeah better be sure and ask to see if you really understood before moving to the next topic of OOP
yeah
oh yes
imagine you learned more about OOP but dont understand the basics
you'Re a very patient person, i appreciate the help and patience with me
its a good quality to have
np :D
you can refuse if you don't want, but would it be possible to keep in touch?
like can I ping you?
if i have other questions
nah others will help too, currently its 4 am (night) in europe and not that much are online but
many can help you like me
so no need for pinging
I will see your questions anyways
you mean others are sleeping
yeah xD
how long have you been learning this?
since 13 iirc
wow how did you enter so young?
so 3/4 years
yeah still in school
how did you learn? self taught?
yeah, germany doesnt provide good knowledge of Computer Science in school
interest
and doing projects is very important
to use the learned stuff
yes, now that i realize, stuff can only make sense by practicing
yeah
nah be happy if you are native english speaker
Im not native english speaker
oh ok
english doesn'T have any patterns
it's just stuff you memorize
anyway, going to go now, good night. im surprised you'Re not sleeping yet though
i live in north america
yes, you too. Im going to read the link tomorrow
