Saltar al reproductorSaltar al contenido principal
En este video se explica el uso de la palabra clave this.

#java #dev #programacion #tutoriales #educacion

Categoría

📚
Aprendizaje
Transcripción
00:00Welcome to this new video.
00:04This video is going to be about the celebratory word DISC.
00:09So what is DISC in Java?
00:14Well, the word DISC is a keyword that is used to refer to the current object of a method or a constructor.
00:28This way of saying it is like... it's the way of saying this or that.
00:37So, the word DISC is used to differentiate between class attributes and constructor parameters.
00:48So let's look at it with this example that we have here.
00:53In this case we have a product class called product with two attributes.
01:00A string called name and an integer called price.
01:06So here we can see that we have a parameterized constructor with two parameters also of type string and integer.
01:19With the same name as the attributes.
01:22So here we have the reserved word DISC.
01:27So what is this word for?
01:32Well, as you can see, if we had the constructor like this, how do we tell it that what we want to save in the name attribute is going to be the constructor parameter?
01:57Well, Java gets confused, so it doesn't know which of the two it is.
02:06So if you put the word DISC.name.
02:11Here we are indicating that we want whatever the name parameter contains to be assigned to the name attribute.
02:28The attribute, this is the one of the class.
02:35So what does DISC mean?
02:37Right here he is telling you this name.
02:43That is, when it says this name it refers to the attribute of the class.
02:49So, once, once already corrected.
02:58And as you can see, down here there is a method that doesn't return anything called show product.
03:06So here we can see that System.2.print is going to display the name on the console.
03:17But, it will show us the name of the parameter in case this method had a parameter equal to name.
03:32If this had been like this, it would show us and this would be here like this.
03:39What would this mean?
03:43It will not only show us the variables, it will show us the attributes of the class, but it will also show us the parameters of the method.
03:57So, I don't want to see the method parameters.
04:04I want to see the attributes of the class.
04:08So, I put the reserved word DISC.
04:15We do the same.
04:18And that's it.
04:20And this is what it would look like.
04:22Here we have another class of students.
04:25Which has two attributes.
04:27Name and age.
04:29With builder.
04:31We have two constructors in this case.
04:34One by default.
04:38And another parameterized with two parameters.
04:44With two parameters.
04:45So what's going on here?
04:47It is the reserved word DISC.
04:49Inside a builder.
04:51Wants to make reference.
04:54That's it.
04:56What he is practically doing.
04:59He's calling another builder.
05:02To another builder.
05:03Of the same class.
05:05In this case.
05:06Call the builder.
05:08Parameterized.
05:09With two parameters.
05:10I mean.
05:11You are calling this constructor.
05:12That's what he's doing.
05:14Since this one.
05:16Builder.
05:17It is not parameterized.
05:19And it's by default.
05:20It does not initialize variables directly.
05:24But.
05:25What he's doing.
05:26It is calling the builder.
05:27That you already initialized them.
05:29And here we have one.
05:34Here we have a method.
05:36To display information.
05:38That shows the information.
05:40Of.
05:41Of the attributes.
05:43Containing people.
05:45And here we can see.
05:48Here we have.
05:50A commented code.
05:52That's why I'm commenting on it.
05:56He's going to mark us.
06:00A mistake.
06:01Well.
06:02And he wonders.
06:03What's going on.
06:04Why doesn't it mark error.
06:05Here he is marking us.
06:07A mistake.
06:08Given that.
06:09This method.
06:10It is static.
06:11And what happens?
06:12With the methods.
06:13Static.
06:14What happens.
06:16The thing is.
06:17The methods.
06:18Static.
06:19They belong to the class.
06:20No to the instance.
06:21Specific.
06:22Therefore.
06:23Doesn't have.
06:24access.
06:25To the variable.
06:26As.
06:27Name.
06:28When trying.
06:29Make reference.
06:30With her.
06:31Putting.
06:32Dis.
06:33Spot.
06:34Name.
06:35I mean.
06:36Yes one.
06:37Arrives.
06:38Yeah.
06:39When one.
06:40Instance.
06:41The class.
06:42Student.
06:43It's not going away.
06:45This.
06:46It's not going away.
06:47To power.
06:48Show.
06:49On screen.
06:50It's not going away.
06:51To power.
06:52Show.
06:53This.
06:54Given that.
06:55The name.
06:56The name.
06:57It's not going to happen.
06:58To find.
06:59Given that.
07:00The method.
07:01Is.
07:02Static.
Sé la primera persona en añadir un comentario
Añade tu comentario

Recomendada