public class TwoSides { int side1, side2 ; public boolean testRightTriangle( int hypoteneuse ) { int side1Squared = side1 * side1 ; int side2Squared = side2 * side2 ; int hypSquared = hypoteneuse * hypoteneuse ; return side1Squared + side2Squared == hypSquared ; } }In this example, which checks if we have a right triangle (the class contains the two short sides of a triangle, and we pass the long side, i.e., the hypoteneuse). We apply Pythagorean's theorem to check if it's a right triangle.
In this example:
We say the scope of instance variables is the entire class definition. The scope refers to the section of code where a variable can be accessed.
The scope for a parameter is simply the method body in which the parameter is located.
Parameter names only have to be unique in a parameter list. For example, we can have two methods testRightTriangle and testScaleneTriangle, both of which have parameter int hypoteneuse. Since the scope is just the method body, we know which hypoteneuse is being referred to.
For example:
public class TwoSides { int side1, side2 ; public boolean testRightTriangle( int hypoteneuse ) { // hypoteneuse refers to parameter above // hypoteneuse is NOT same as testScaleneTriangle } public boolean testScaleneTriangle( int hypoteneuse ) { // hypoteneuse refers to parameter above // hypoteneuse is NOT same as testRightTriangle } }Each method has its own hypoteneuse and they do not affect each other.
The scope begins at the point of declaration, and lasts to the end of the method body (this isn't exactly true, but it's true in the example above). You must declare a variable before use.
Again, more than one method can define the same name for a local variable. For example, we could have both testRightTriangle() and testScaleneTriangle can both have a local variable temp. These are not related variables. Each has its own box, and you should think of them as completely different, even though they share the same name.
That means if you make a call to testRightTriangle() and run the declarations, it creates new boxes and initializes them. When you exit, those local variables disappears.
When you call it a second time, it recreates the local variables, and reinitializes them.
Parameters behave somewhat like this. These variables gets initalized to the arguments.
Only instance variables maintain their value over method calls. That is, if some instance variable has value of 4 for height, and you change it to 10, then the next time you make a method call, it's still 10. It stays 10 until some method call changes the value.
Local variables also have a similar lifetime. The boxes are created when the declaration appears, and lasts until you exit the method.
Instance variables have a much longer lifetime. They are created when the object is constructed, and goes away when the object disappears. The object disappears when it is no longer being used, which basically means that no variable has a handle to the object. When that happens, the garbage collector gets rid of the object.
The garbage collector is a program that runs when your program runs, and looks for objects (balloons) with no variables that have handles to it.
Lifetime refers to the amount of time a variable (or object) exists. Since object variables are both a box (holding a handle) and an object, the lifetimes may differ. An object variable could disappear (for example, a local variable disappears once you exit the method where the local variable is declared), yet the object still exists.
We can divide lifetime into categories too: