From the course: Learning Java 11
Unlock the full course today
Join today to access over 24,000 courses taught by industry experts.
Instance methods vs. class methods - Java Tutorial
From the course: Learning Java 11
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…
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)
Classes in Java4m 55s
-
(Locked)
Constructors in Java6m 50s
-
(Locked)
Creating a class in Java3m 57s
-
(Locked)
Creating instances in Java4m 57s
-
(Locked)
Instance methods vs. class methods2m 52s
-
(Locked)
Using instance methods in Java2m 47s
-
(Locked)
Instance and class variables in Java3m 56s
-
(Locked)
Review: Classes vs. instances1m 57s
-
(Locked)
Solution: Bank account balance1m 19s
-
(Locked)
-