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.
Instance and class variables in Java - Java Tutorial
From the course: Learning Java 11
Instance and class variables in Java
- [Instructor] With the findArea call we've used the dot-operator to access a behavior or method from our Triangle class. What about attributes? So far, we have only accessed a triangle's instances values using the this keyword inside it's class. But, we can also access it outside it's class. In our main method we could print out triangle A's, Sidelenthree with system.out.printLn, trianglea, sideLenThree. We use the .operator on trianglea, to access it's instance variable. Or attribute variable, sidelenthree. We could also print out triangleb's base with system.out.println triangleb.base The rule here is that it is a function if it has parens and an attribute if it does not have parens. Sidelenthree and base are both attributes here. So, we do not add any parens. We are accessing a variable, not calling a function. Let's save this and run the program. And, see what we get in the output. The last two values 17 is the length of side three. And, three is the base of triangleB. Remember…
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
-
-
-
-
-
-
-
-
Classes in Java4m 55s
-
Constructors in Java6m 50s
-
Creating a class in Java3m 57s
-
Creating instances in Java4m 57s
-
Instance methods vs. class methods2m 52s
-
Using instance methods in Java2m 47s
-
Instance and class variables in Java3m 56s
-
Review: Classes vs. instances1m 57s
-
Challenge: Student profile2m 1s
-
Sample solution: Student profile3m 26s
-
-