In this course, we'll learn the object-oriented concept in Java.
- Object-Oriented programming concept
- Object & Class
- Access Modifiers
- Naming Conventions
- Static Keyword
- Nested and Inner Classes
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there. In this video, we will talk about the constructor in Java. Constructor is similar to a method. It has the same name as its class. When a new object is created, at least one constructor will be invoked. Every class has a constructor that is named the default constructor. However, constructors have no explicit return type. As you see in our example, a class can have more than one constructor. In order to understand the working of constructors, let's take an example. Let's say we have a class Car. When we create two objects of Car, in creating the 1st object, the new keyword here creates the object of class Car and invokes the default constructor that has no parameter to initialize the newly created object.
In creating the 2nd object, also, the new keyword creates the object of class Car and invokes the constructor has one parameter to initialize the newly created object. Let's make some examples with constructors. First, let's create a new Java project and specify the project name as ObjectOriented. Don't change the other options, and click the 'Finish' button. In this project, right-click on the 'Source' folder and select New Class. Specify the package name as oopconcept. And the class name can be Car. And this time, I will not check the check box for the main method. I'll create only a Java class. So, I click the 'Finish' button. Okay, now we have a Car class. The states of the Car are model and currentSpeed.
We declare them with a private access modifier because I don't want to access these variables from other classes. So, I write private String model, private int currentSpeed. Now let's declare a public constructor named Car. And it takes one parameter and its name is model. We declare the constructor as public because we want to access this constructor from the other classes. Constructors are usually public because the purpose of this is to create the object of this class from different classes. Also notice that the constructor has no return value. In the constructor method, I'll assign the model parameter here to the own variable of this class, i.e. model. To do this, we use the this keyword in the constructor. Don't worry, we'll learn about the this keyword soon. Let's continue now. Here I write, this.model = model.
So, the this keyword represents the Car class, and the model here represents the model variable in this class. The purpose of this parameter is, that when we want to create an object from the Car class, we will need to write a value corresponding to the parameter we want here; just like when we call a method with the same parameter in a different place, we give its parameter. In other words, thanks to this parameter, we'll be able to assign a value to the model variable of this class from a different class. You can also write a different name here. For example, let it be the Car model. However, in general usage, the parameter name is the same as the variable name. Let's continue now. Let's declare four public methods of the Car class. We declare it as public because we want to access these methods from other classes.
The name of this 1st method is start with void type. In the start method, we'll use the print method to display the car has started message by adding the variable model. So, I will write S out this.model + "has started." The name of the 2nd method is accelerate. We'll add 20 mph to the currentSpeed variable by using the add an assignment operator. So, I will write this.currentSpeed + = 20. The name of the 3rd method is stop. In the stop method, we will use the print method to display the car has stopped message by adding the variable model. So, I write S out this.model + "has stopped." Also, I'll set the currentSpeed to zero, because if the car stops then its speed will be zero. So, I write this.currentSpeed = 0.
Okay, the name of the last method is showSpeed. In the showSpeed method, we use the print method to display the current speed of the car by adding the variables model and currentSpeed. So. I write S out "The current speed of." After the plus sign, I write this.model and plus sign again. Inside the double quotes, I write is. And last, this.currentSpeed. So, the console message will be like that. The current speed of Ferrari is 20. Okay, let's save the code. Okay, we need to test this class. Let's create a new class. Right-click on the 'Oopconcept' package and select New Class. Specify the class name as CarTest, and select the check box for the main method. Let's create an object of the Car class by using the new keyword: Car myCar = new Car. We pass Ferrari as a parameter to the Car class. This process invokes the constructor that has one argument.
Let me explain this briefly. The new keyword here actually invokes the constructor method in the Car class. Note that in the constructor method, there is a string type parameter. Therefore, we need to assign a value corresponding to this parameter in the object creation process. So, we wrote Ferrari. Then this Ferrari object will be transferred to the car model variable in the constructor method. If you pay attention, the variable car model will also be transferred to the model variable of this class. In other words, as a result, the Ferrari is transferred to the model variable in the Car class with the help of the constructor. Now, let's call the methods we defined in the Car class by using the myCar object. We can access the properties of an object with a simple dot notation. If we write myCar., eclipse automatically brings the properties of class, and we choose them to add to our code.
We add the start method first, then accelerate, then showSpeed, and then stop, and again showSpeed. Okay, let's run code in the test class. You see the results in the console. Start method is called and it displays the Ferrari has started message. Accelerate method is called and set 20 to the currentSpeed variable. ShowSpeed method is called and it displays the current speed of Ferrari is 20. Stop method is called and it displayed Ferrari has stopped and set zero to the currentSpeed variable. Last, showSpeed method is called again, and it displays the current speed of Ferrari is zero. Okay, now you are ready to learn the toString method. But before we move on to the toString method, I want to show you this. First, let's print the myCar object here to the console with the println method. Now, let's run the application once again.
As you can see, an ID is printed on the console screen. Let's examine this. Notice first the package name. The package referenced by the myCar object, then the class name. The class referenced by the myCar object and then an ID. If you remember, we talked about stack and heap in our previous lessons. Here, the object of myCar is created in the stack and has an ID. But also references the memory reserved for the Car class in the heap. Now, let's move on to the toString method and see how it will change our code. Let's declare two variables in the Car class. The 1st one is color with string type, and the 2nd one is year with int type. Let's declare another constructor named Car. And it takes three parameters: model, color, and year. In the constructor method, we assigned the model to its own variable model using the this keyword: color to own variable, color by using the this keyword. And year to own variable, year by using the this keyword.
Now, let's declare the toString method of the Car class. ToString method return string. We return name, color, and year variables by using the plus operator in this method. Model: this.model; color: this.color; year: this.year. Okay, let's save the code. Okay, let's go to the test class: CarTest. And let's create a new object. Its name is myCar1. And as you can see this time, two constructors of the Car class are offering us. This is for the one parameter, this is for the three parameters. I will choose this constructor this time. We passed three parameters to the Car class. The 1st parameter will be the model. So, I write Ferrari. The 2nd parameter will be the color. So, I write red. The last parameter will be the year. So, I write 2022. Now, let's print the myCar1 object to the console and see the difference before and after the toString method.
Because this time, we will not see any ID on the console. If you print any object directly, the Java compiler internally invokes the toString method if it's created. Otherwise, it prints the objects ID in stack memory. Okay, let's run the test class again. You see the result in the last statement. ToString method is called and it prints model is Ferrari, color is red, and year is 2022. Let's open the Car class. We invoked this constructor and toString method of the Car class. Yes, this is the constructor in Java. In short, constructors have no return value. They have the same name as the class they are in. When a new object is created, at least one constructor will be invoked. You can create multiple constructors. If you don't create any constructor, Java automatically creates an empty constructor. Yes, let's take a short break here. See you in the next lesson.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.