From the course: Learning Java 11

Unlock the full course today

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

Instance methods vs. class methods

Instance methods vs. class methods - Java Tutorial

From the course: Learning Java 11

Start my 1-month free trial

Instance methods vs. class methods

- [Instructor] Let's try calculating the area of each triangle. We're store the area of triangle A in a variable called triangleAArea, and it will be a double, because that's what the findArea function returns. To get access to the findArea function, we use the dot operator on the triangle A instance. So we'll write triangleA.findArea. Now you might be thinking, why didn't we write triangle.findArea? Isn't that what we did when we used pow on the math class with math.pow? And yes, this is where a lot of people get confused. The reason we did triangleA.findArea instead of triangle.findArea is because in order to find the area of a given triangle, you have to have a triangle instance. You can't calculate the area of a triangle that doesn't exist yet. The implementation of the findArea function relies on the attributes of a given triangle. The base might be eight or 10 or 15. We don't know until the triangle is actually created. Because we have to have a triangle instance already created…

Contents