From the course: Learning Java 11

Unlock the full course today

Join today to access over 22,500 courses taught by industry experts or purchase this course individually.

Creating a class in Java

Creating a class in Java - Java Tutorial

From the course: Learning Java 11

Start my 1-month free trial

Creating a class in Java

- [Instructor] With an understanding of classes, instances and the constructor, we can begin to write Java Code that helps us represent a triangle in a program. First, we need to write code that defines our triangle blueprint. And we can do that with the triangle class. In Java, We'll create a new class file and name it triangle. Inside the curly braces for our class, we'll need to add the attributes and behavior we want the class to have. In earlier lessons, we said that a triangle has a base, height and three different side links. We can add those inside of our class. We now have five attribute variables that we have created, but they do not have a value yet. These attribute variables are also called instance variables because they are variables that each instance will have an individual value for. We could assign a default value to these instance variables, but as we discussed in the last lesson, usually, we want a constructor to initialize them. With the constructor, we can create…

Contents