From the course: Java Essential Training: Syntax and Structure

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Code Solution: Create a car object

Code Solution: Create a car object - Java Tutorial

From the course: Java Essential Training: Syntax and Structure

Code Solution: Create a car object

(upbeat music) - [Instructor] In this challenge, your task is to build a car. You were provided with the car class, which serves as a blueprint containing the attributes of a car. In the buildsCar method, this method accepts details about a specific car, allowing you to construct an instance of the car class. In fact, let's go ahead and do that now. So we create an object of type car, and we can name it Car as well. And to create an instance of the car class, we'll use the new keyword, and we'll call the car constructor, which accepts the make and the model. So now we have a car, and we have the make and the model specified. However, we also need to include the year and the color. We can do that by calling the car setter methods. So we can say car.setYear, and we'll pass in the year, and then car.setColor, and we'll pass in the color. Finally, we'll go ahead and return the car object. And let's go ahead and test this. Great, it worked.

Contents