#How does one use this() in this example?

1 messages · Page 1 of 1 (latest)

jade gateBOT
#

<@&987246399047479336> please have a look, thanks.

jade gateBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

torn citrus
#

I was given this example, I'm learning OOP as a beginner. What exactly does "this" do here?

tribal sun
#

first: This(h) should be this(h)

#

and for your question:

#

you know what constructors are?

torn citrus
#

I just learned today what they are

tribal sun
#

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

torn citrus
#

of the second "Rectangle" right?

tribal sun
#

you mean the second constructor of Rectangle?

torn citrus
#

yes

tribal sun
#

yeah so in this example this(h) just calls the other constructor in this class which takes in only one double value

torn citrus
#

no

tribal sun
#

but this can also be used differently

torn citrus
#

"this(h);" refers to this line "public Rectangle(double h, double l)" ?

#

this is the current instance no?

tribal sun
#

no it is in this constructor

#

but it calls the other constructor

torn citrus
#

Ok I don't get it then

#

Why is is calling the other constructor?

#

if it is the current instance?

tribal sun
#

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

torn citrus
#

@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?

tribal sun
#

they need to be different, this means they need to have different parameter

torn citrus
tribal sun
#

yeah so this(h) calls the first because first of all h is a double and there is one constructor which accepts a double

torn citrus
#

well both of them use doubles, but only of them has one paremeter with one double

tribal sun
#

yeah exactly there is only one constructor which only takes one double

torn citrus
#

ok, so if I said this(x) and this(x,y) within the same cosntructor, those two would refer to two different constructors, right?

tribal sun
#

yeah

#

this is a good explanation on the this keyword with a constructor

torn citrus
#

link?

tribal sun
#

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.

torn citrus
#

I have two other questions btw

tribal sun
#

go on

torn citrus
#

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?

tribal sun
tribal sun
# tribal sun this is a good explanation on the this keyword with a constructor

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;
    }
torn citrus
#

those four variables refer to the global variables inside the class right?

tribal sun
#

yeah as said this can also be used to distinct local variables with fields

torn citrus
#

im going to read yout link tomorrow, going to go sleep that'S why I don't have time

tribal sun
#

ok np, for more questions just ask

#

gn

torn citrus
#

not now

#

but I have been teaching myself becaus my tacher sucks

#

teacher*

tribal sun
#

oof hard times

torn citrus
tribal sun
#

yeah

torn citrus
#

you mean "this" can be used as an instance of a current constructor?

tribal sun
#

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.

torn citrus
#

yes this one i actually understood before

torn citrus
tribal sun
#

I mean that with that sentence

torn citrus
#

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

tribal sun
#

this.x refers to a field in the current object you are in

#

you know what a field is?

torn citrus
#

no

#

i assume its variables?

tribal sun
#

also known as members of a class

torn citrus
#

and what are those members?

#

variables and methods?

tribal sun
#

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;
    }
    ...
}
torn citrus
#

ah ok, so those are teh varaibles of the class

tribal sun
#

yeah

torn citrus
tribal sun
#

yeah

torn citrus
#

ok

tribal sun
#

no wait

#

I think I missunderstood your sentence

torn citrus
#

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

tribal sun
#

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.nameOfField refers to a field of the current class named nameOfField

tribal sun
#

this always refers to the current class

#

also works with method calls

torn citrus
#

as we said before

tribal sun
torn citrus
#

yes

tribal sun
#

yeah

torn citrus
#

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?

tribal sun
#

yeah, the one that accepts 4 integers

torn citrus
#

ok, and when it calls this constructor, what happens afterward?

#

what is it doing exactly?

tribal sun
#

it just created the object like a normal constructor

torn citrus
#

an object with 4 values?

tribal sun
#

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

torn citrus
#

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?

tribal sun
#

yeah

torn citrus
#

yes ok

#

good

#

This line I Meant (int width, int height)

#

it will receive those

tribal sun
#

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

torn citrus
#

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

tribal sun
#

yeah

torn citrus
#

so, my object at the end will have the following hauteur = h and largeur = l

tribal sun
#

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

torn citrus
tribal sun
#

yeah

torn citrus
#

ok thanks 1000 times

#

for your patience

tribal sun
#

np

torn citrus
#

you're a real pro bro

#

i know i asked 1 million questions

tribal sun
#

OOP can be very confusing

torn citrus
#

i learned all fo this today, and my teacher genuinely is bad

#

so i have to guess sometimes

tribal sun
#

yeah better be sure and ask to see if you really understood before moving to the next topic of OOP

torn citrus
#

wdymen

#

you mean like I did now?

tribal sun
#

yeah

torn citrus
#

oh yes

tribal sun
#

imagine you learned more about OOP but dont understand the basics

torn citrus
#

you'Re a very patient person, i appreciate the help and patience with me

#

its a good quality to have

tribal sun
#

np :D

torn citrus
#

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

tribal sun
#

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

torn citrus
#

are you from europe?

#

oh

tribal sun
#

I will see your questions anyways

torn citrus
#

you mean others are sleeping

tribal sun
#

yeah xD

torn citrus
#

how long have you been learning this?

tribal sun
#

since 13 iirc

torn citrus
#

wow how did you enter so young?

tribal sun
#

so 3/4 years

torn citrus
#

oh

#

you'Re in your late teens

#

wow thats good

tribal sun
#

yeah still in school

torn citrus
#

how did you learn? self taught?

tribal sun
#

yeah, germany doesnt provide good knowledge of Computer Science in school

tribal sun
#

and doing projects is very important

#

to use the learned stuff

torn citrus
#

yes, now that i realize, stuff can only make sense by practicing

tribal sun
#

yeah

torn citrus
#

like getting your hands dirty

#

btw I wish i can learn german one day

tribal sun
#

nah be happy if you are native english speaker

torn citrus
#

Im not native english speaker

tribal sun
#

oh ok

torn citrus
#

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

tribal sun
#

xD

#

ok gn and good luck with computer science cat_thumbs_up

torn citrus
#

yes, you too. Im going to read the link tomorrow