From the course: Learning Java 17

Creating an instance in Java - Java Tutorial

From the course: Learning Java 17

Creating an instance in Java

- [Instructor] Let's create some triangle instances in our Java code. Continuing our program from the last lesson, we'll create a main class with a Main method. We could add a method to the triangle class and run that file instead, but I find the separation of the blueprint and the code we're actually running makes it easier to understand at first. To create a triangle, we'll create a triangle variable. We'll call it triangleA. We'll give it a value using the triangle constructor, new Triangle, and will feed in some attribute values. The base will be 15, the height 8, 15, 8, and 17 for the sideLengths, and we'll hit enter here to separate it up. Then we'll create a triangleB. It'll have the data type Triangle, named triangleB. We'll reuse that constructor, but with different attribute values. So 3, 2.598, and then 3, 3, and 3. So we have two triangle instances. Let's add a breakpoint and walk through the program so we can see what's going on behind the scenes. We'll add a breakpoint on line four just before we create triangleA. Let's run it in Debug mode. All right, so we're about to create our triangle instance. We're passing in 15 for the base, 8 for the height and the sideLengths. Let's Step Into the triangle call, the call to the triangle constructor. Now we're in the constructor itself where we'll be creating the triangle instance that will be stored in triangleA. Our parameters have the values that we passed in for the input. The base is 15, the height is 8, sideLengthOne is 15, 8, and 17. In the following lines of code, we'll assign these values to the attributes of the triangle that's currently being created. All of our attributes for this new triangle have defaulted to zero. That's what we see up here with the magic of the IDE. In the constructor, we'll initialize each attribute for this triangle we're creating. So we'll hit Step Over, Step Over. Base now has the value 15, and we see that up here with the attribute. We'll do same with the height, sideLengthOne, Two and Three. Each attribute is getting assigned the value we passed into the constructor. In the variables window at the bottom, if we open the this pane, this is the triangle that we're constructing. As we initialize it, the values within this triangle object are initialized. Let's hit Step Over, and we'll return to the main class. Now it's time to initialize triangleB. Let's Step Into this triangle call. Using the constructor again, we're creating yet another triangle instance. This triangle instance, like the other, has its default attribute values set to zero. They are not connected to the attribute values of triangleA because different triangles can have different values for their base, height, and side lengths. Each instance created from the constructor is independent of each other. The attribute values of one triangle do not affect the other. Let's initialize this triangle, the triangle that will be stored in triangleB. Base will get the value 3 starting at zero, zero here as well. We'll hit Step Over, and we're starting to initialize the triangle. Let's initialize each attribute. We initialize the height, the sideLengthOne, Two, and then Three. At the end of this constructor's execution, all of the attributes for this triangle have been initialized, and now this newly created triangle instance is stored in triangleB. We've created two triangle instances using the constructor from our triangle class. Our program not only knows how to represent triangles but it's built to triangle objects.

Contents