From the course: Learning Java 17

Unlock this course with a free trial

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

What is a constructor?

What is a constructor? - Java Tutorial

From the course: Learning Java 17

What is a constructor?

- [Instructor] In order to use a function, it has to be defined first. The same is true with classes. We have to define a blueprint or a class in order to use it. In the last lesson, we defined the Triangle class but we didn't create any triangles yet. We defined the attributes and behavior in a blueprint. In this lesson, we're going to learn how to create individual Triangle instances in our program. An instance is an object created from the class blueprint. Just like we make buildings from blueprints in real life, we use a class blueprint to create objects or instances in code. Thinking back to our Triangle class, a triangle has a base, height and three side lengths. Another instance of a Triangle could have base, 3, height, 2.598 and side lengths, three, three and three. From our single Triangle class, we can create as many instances as we want. Each instance will have its own base, height and side length…

Contents