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

Instance and class variables in Java - Java Tutorial

From the course: Learning Java 11

Start my 1-month free trial

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…

Contents