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? - 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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
-
(Locked)
What is a class?4m 10s
-
(Locked)
What is a constructor?6m 14s
-
(Locked)
Creating a class in Java4m 24s
-
(Locked)
Creating an instance in Java5m
-
(Locked)
Instance versus class members3m 25s
-
(Locked)
Using instance and class methods in Java3m 14s
-
(Locked)
Instance and class variables in Java4m 16s
-
(Locked)
Reviewing classes versus instances2m 6s
-
(Locked)
Solution: Book name1m 19s
-
(Locked)
-