课程: Learning Java 17
Instance and class variables in Java
- [Instructor] With the findArea call, we used the dot operator to access the behavior for a specific triangle instance. What about accessing attributes? We've used this keyword inside the Triangle class to access these attributes, but we can also access them outside the class. In the main method, we can print out triangle A's side length three. System.out.println will access triangle A and use the dot operator to print out side length three. We can also print out triangle B's base. System.out.println will access triangleB.base. The rule here is it has a parentheses if it's a function. FindArea is a function. So we have those parens. For attributes, we don't add parentheses. Side length three and base are both attributes. So no parentheses are added. Let's run it. For triangle A, we get side length 17 and for triangle B, we get three. This is what's expected. Both side length three and base are instance variables. They depend on the values of the instance. It's important to note that both of these attributes hold information about specific triangle instances. Just like we had instance in class functions, we have instance in class variables. We refer to our instance variables as non-static because they do not stay static or the same between triangle instances. The values of base, height and the side lengths change depending on the triangle. A static variable is something that will not change per instance. They hold a value for the whole class to use. Hence, the name variable. We might use a static variable to store how many sides there are to a triangle. It's always three. This is something that will not change per instance, but rather if you change it, it should affect every instance and every triangle. We can access a static variable using the class name. Since it belongs to the class, rather than a single instance, a static variable is accessed with the dot operator using the class name. Let's try adding a static variable to our triangle class. Inside the triangle class, we'll add a static variable above our instance variables. The static variable will be an int, and it'll be called numOfSides. We'll give it the value three. To denote it as static, we use the static keyword. Going back to the main class, we can access the static variable with the class name, triangle. We'll print out the number of sides. System.println. We'll use the class name, triangle, with the dot operator and then the variable name, numOfSides. Let's run it. There's three in our console. Of course, this representation of a triangle isn't perfect. Right now we assume the values inputted to the construction are valid. None of them are zero or negative, and together they meet the requirements for a triangle. This is something you can add later if you want to, but we're keeping this as simple as possible for now. In this chapter, we've introduced a lot of new concepts. So next, we'll take some time to review what we've learned.
随堂练习,边学边练
下载课堂讲义。学练结合,紧跟进度,轻松巩固知识。
内容
-
-
-
-
-
-
-
-
What is a class?4 分钟 10 秒
-
What is a constructor?6 分钟 14 秒
-
Creating a class in Java4 分钟 24 秒
-
Creating an instance in Java5 分钟
-
Instance versus class members3 分钟 25 秒
-
Using instance and class methods in Java3 分钟 14 秒
-
Instance and class variables in Java4 分钟 16 秒
-
Reviewing classes versus instances2 分钟 6 秒
-
Solution: Book name1 分钟 19 秒
-
-