From the course: Learning Java 17
Reviewing classes versus instances - Java Tutorial
From the course: Learning Java 17
Reviewing classes versus instances
- So, what are the big takeaways from this chapter? First, we can create classes in Java to organize our code. In the main example, we used a triangle class to organize data related to a triangle. We also used it to calculate things with triangle data. A class contains attributes and behavior. Every class also contains a constructor, which is a specific type of behavior that allows us to create instances of the class. Ultimately, a class is just a blueprint and we have to use a constructor in order to create instances in our code. Some of these attributes are associated with each individual instance, and we call these instance variables. These were the height, base, and side length attributes. in our example. They were accessed through the instance itself and we used the dot operator on the instance to access these variables. Along with instance variables, each instance also had instance functions or non-static functions. Similarly, these are accessed using the instance, with the dot operator and parentheses. We also have class variables and class methods. These are static, and we access them with the class name and the dot operator. Since they do not vary from instance to instance, we do not need to create an instance in order to use them. Now, this new vocabulary can sometimes be overwhelming, but when you're debugging, Google searching, and trying to figure out how to fix your code, these words often come up. That's why I've brought them up here. Understanding these concepts is a big hurdle in Java, but once you get it, everything starts to make more sense.
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
-
-
-
-
-
-
-
-
What is a class?4m 10s
-
What is a constructor?6m 14s
-
Creating a class in Java4m 24s
-
Creating an instance in Java5m
-
Instance versus class members3m 25s
-
Using instance and class methods in Java3m 14s
-
Instance and class variables in Java4m 16s
-
Reviewing classes versus instances2m 6s
-
Solution: Book name1m 19s
-
-