‫Welcome back.
‫In this video, we'll talk about constructors in Java.
‫A constructor is a special method that it is used to create and initialize objects when they are first
‫created.
‫It's like a magical process that gives life to your objects and sets them up with the initial values
‫they need.
‫Let's continue with the car example.
‫To create a constructor, we have to name it as the class name the class here car Inside its scope.
‫We need to define the constructor so I'll make here constructor and starting with the public keyword.
‫Later on we'll in the next videos we'll talk about modifiers and access modifiers.
‫But now focus with me on the constructors car exactly as the name of the class.
‫Pay attention please guys.
‫And followed by the these parentheses because it's a method, a special method when creating the object
‫of a car, we need to define these attributes.
‫Let me show you.
‫We have two states the year and the speed.
‫I'll pass them here in speed and int year.
‫Then in the constructor we set here, I'll name it as car speed and car year.
‫Here we set the year equal to car year and the speed equal to car speed.
‫So these two parameters are like placeholders where we'll put the actual values when we create a car
‫object.
‫So when we create an object of the car, we define its speed and its year and set them to the values
‫of these states.
‫Now let's create a car object using this constructor.
‫Let's go up and here we have an error because we need to pass two arguments expected two arguments,
‫but found zero.
‫So we need to pass the arguments of the speed and the year of this car.
‫So I remove this attribute and now I'll start with the car speed, which is 100 and the car year, which
‫is 2023.
‫Please, guys, focus with me.
‫These are hints.
‫So these are auto generated hints by Android studio.
‫You don't need to put and write car speed keyword.
‫Just put 100 and just put 2023.
‫Let's run our program.
‫And as expected, we created an object called Red Car with a car speed 100.
‫We accelerated by ten, then by another ten, and then we brake, removing five from its speed.
‫So this is how we use constructors in Java.