Solution Review: Reference and Local Variables
The solution to the Reference and Local Variables exercise is explained in this lesson.
We'll cover the following...
Coding solution
Let’s take a look at the solution first.
Press + to interact
public class RLVariables {private int mySecretNumber = 10;public void displaySecret() {mySecretNumber = 45;RLVariables secret = new RLVariables();System.out.println("Reference variable accessing instance variable: " + secret.mySecretNumber);System.out.println("Local Variable: " + mySecretNumber);secret.mySecretNumber = mySecretNumber;System.out.println("Reference variable accessing instance variable that points to the local variable: " + secret.mySecretNumber);}public static void main(String [] args){RLVariables notSoPrivate = new RLVariables();notSoPrivate.displaySecret();}}
Explanation
This output is expected. We can ...