Section 4.15 Worked Example: Assignment of Object References
Subgoals for using objects (creating instances).
Subsection 4.15.1
Recall our class
Widget from Section 4.5. It is a made-up class for this chapter. We will reuse it in this section. The developers who defined the Widget class provided the following constructor documentation (same as section 4.5).

Remember that because we do not have an implementation of the
Widget class, the ActiveCode block below will not work with the Widget class.
The
Widget class also have a few additional methods that will help us with this section:

Subsection 4.15.2
We have been given the following code that uses the
Widget class and want to determine the values of delta and gamma after the code executes.
Widget alpha = new Widget(5);
Widget beta = alpha;
alpha.setBibs(16);
int delta = alpha.getBibs();
int gamma = beta.getBibs();
Subsection 4.15.3 SG1: Declare variable of appropriate class datatype
Subsection 4.15.4 SG2: Assign to variable: keyword new, followed by class name, followed by (). SG3: Determine whether parameter(s) are appropriate (API)
A new
Widget() is assigned to alpha with a parameter of 5, which is an appropriate type (int) based on the API.
Subsection 4.15.5 SG1: Declare variable of appropriate class datatype.
Subsection 4.15.6
beta is not assigned a new isntance of a Widget object. Instead beta is assigned the same value alpha. To see what happens inside the computer, we have the following image:

Note that both beta and alpha refer to the same object inside memory.
Subsection 4.15.7 Calling Methods
The next line calls a method on
alpha.
alpha.setBibs(16);
Recall our subgoals for calling a method. We saw that this method existed in the
Widget class in section 4.14a.1. It is an instance method and we have an instance of Widget named alpha. The method setBibs takes in an integer parameter and we have passed the value 16 to the method call, which is an integer. The method setBibs does not return a value after it executes, so we do not need to store the result of calling this method.
Based on the documentation for
Widget, we know that there is a number of bibs stored in the Widget alpha that is being set to 16.

Subsection 4.15.8 Calling Methods
Our next line calls another method on
alpha.
int delta = alpha.getBibs();
Recalling our subgoals for calling a method once again. We know that this method also exists in the
Widget class based on the information in section 4.14a.1. It is an instance method and we are calling it on our alpha instance of Widget. The method getBibs does not take any parameters. However, it does return an integer value and we are storing that value in the integer variable delta.
Based on the documentation for
Widget, this method returns the number of bibs stored inside the Widget. Since we just set that number to 16 in the previous line, this method will return that same value and the value assigned to delta is 16.
Subsection 4.15.9 Calling Methods
Our last line calls a method on
beta.
int gamma = beta.getBibs();
This line is very similar to the last in that it calls the method getBibs on a
Widget object and assigns the returned value to a variable named gamma. We now have to determine what value the method call returns. Remember that we saw that alpha and beta point to the same object inside memory.

When we changed the value of bibs for
alpha, it changed for beta as well. So, gamma is assigned the value 16.
The more formal name for what is happening here is called aliasing. In this example,
beta is an alias for alpha (and vice versa). They point to the same object in memory, so doing something to one causes the other to change.
Subsection 4.15.10
When two reference variables point to the exact same object in memory, the variables are an alias of one another. And if the object changes, that change is reflected in both reference variables. Another notion is having multiple reference variables (each instantiated separately) but they pull or generate data from the same source.
-
Scanner: you should never have multiple instances of a Scanner reference that use the same input (like System.in). This would cause reading from the same input stream by two different reference variables which can cause confusion. Imagine two different people accessing the same conveyor belt for parts but you never know which person is going to pick up which parts! Only create separate Scanner instances if you have separate input streams (like System.in and a file).
-
Random: if you create two reference variables of the Random class, but instantiate them using the same seed or starting value, then you will get the identical set of Random numbers. This likely would not have the "randomness" the algorithm requires.
Also note that the next section is about String objects which behave differently than other objects because they are immutable. Once a string has been created it can never be changed - an entirely new String is created. Therefore, even if two String reference variables point to the same String, if the String is changed then a new String is created:
String alpha = "Hello";
String beta = alpha; // Diagram a
alpha += " Goodbye"; // Diagram b
System.out.println(alpha);
System.out.println(beta);

Output is:
Hello Goodbye
Hello
You have attempted of activities on this page.
