It's time to test that the Character class is a reference type:
- Declare a new Character variable in LearningCurve, called hero2. Assign hero2 to hero, and use the PrintStatsInfo method to print out both sets of information.
- Click on Play and take a look at the two debug logs that show up in the Console:
Character hero = new Character();
Character hero2 = hero;
hero.PrintStatsInfo();
hero2.PrintStatsInfo();
- The two debug logs will be identical because hero2 was assigned to hero when it was created. At this point, both hero2 and hero point to where hero is located in memory:
- Now, change the name of hero2 to something fun and click on Play again:
Character hero2 = hero;
hero2.name = "Sir Krane the Brave";
You'll see that both hero and hero2 now have the same name, even though only one of our character's data was changed....