bag1
and bag2
— to refer to two different BagOfCheese
objects. That's fine. But sometimes, having only one variable and reusing it for the second object works just as well, as shown here.package com.allmycode.a09_03;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.allmycode.a09_01.BagOfCheese;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
<strong> BagOfCheese bag = new BagOfCheese();</strong>
bag.kind = "Cheddar";
bag.weight = 2.43;
bag.daysAged = 30;
bag.isDomestic = true;
textView.setText("");
textView.append(bag.kind + ", " + bag.weight + ", " +
bag.daysAged + ", " + bag.isDomestic + "\n");
<strong> bag = new BagOfCheese();</strong>
bag.kind = "Blue";
bag.weight = 5.987;
bag.daysAged = 90;
bag.isDomestic = false;
textView.append(bag.kind + ", " + bag.weight + ", " +
bag.daysAged + ", " + bag.isDomestic + "\n");
}
}
In this code, when Java executes the second bag = new BagOfCheese()
statement, the old object (the bag containing cheddar) has disappeared. Without bag (or any other variable) referring to that cheddar object, there's no way your code can do anything with the cheddar object. Fortunately, by the time you reach the second bag = new BagOfCheese()
statement, you're finished doing everything you want to do with the original cheddar bag. In this case, reusing the bag variable is acceptable.
When you reuse a variable (like the one and only bag
variable above), you do so by using an assignment statement, not an initialization. In other words, you don't write BagOfCheese bag
a second time in your code. If you do, you see error messages in the Android Studio editor.
To be painfully precise, you can, in fact, write BagOfCheese bag
more than once in the same piece of code.
BagOfCheese
class's fields is final
. In other words, the class's code lets you reassign values to the fields inside a BagOfCheese
object. With this information in mind, you can shorten the code above by one more line, as shown here.package com.allmycode.a09_04;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.allmycode.a09_01.BagOfCheese;
public class MainActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
BagOfCheese bag = new BagOfCheese();
bag.kind = "Cheddar";
bag.weight = 2.43;
bag.daysAged = 30;
bag.isDomestic = true;
textView.setText("");
textView.append(bag.kind + ", " + bag.weight + ", " +
bag.daysAged + ", " + bag.isDomestic + "\n");
<strong> // bag = new BagOfCheese();</strong>
bag.kind = "Blue";
bag.weight = 5.987;
bag.daysAged = 90;
bag.isDomestic = false;
textView.append(bag.kind + ", " + bag.weight + ", " +
bag.daysAged + ", " + bag.isDomestic + "\n");
}
}
With the second constructor call in this code commented out, you don't make the bag
variable refer to a new object. Instead, you economize by assigning new values to the existing object's fields.
In some situations, reusing an object's fields can be more efficient (quicker to execute) than creating a new object. But whenever you have a choice, it’s a good idea to write code that mirrors real data. If an actual bag's content doesn't change from cheddar cheese to blue cheese, it would be better not to change a BagOfCheese
object's kind field from "Cheddar
" to "Blue
".