Creating instances of classes and understanding their scope
We will write a few lines of code that create an instance of the Rectangle class named rectangle within the scope of a getGeneratedRectangleHeight method. The code within the method uses the created instance to access and return the value of its height field. In this case, the code uses the final keyword as a prefix to the Rectangle type to declare an immutable reference
to the Rectangle instance named rectangle.
Note
An immutable reference is also known as a constant reference because we cannot replace the reference held by the rectangle constant with another instance of Rectangle.
After we define the new method, we will call it and we will force a garbage collection. The code file for the sample is included in the java_9_oop_chapter_03_01 folder, in the example03_15.java file.
double getGeneratedRectangleHeight() {
final Rectangle rectangle = new Rectangle(37, 87);
return rectangle.height;
}
System.out.printf("Height: ...