Chapter 12

Answers to Chapter 12 Review Questions

1: You are writing a Planet class for a solar system simulation program. You need to keep track of the number of instantiated Planet objects. If you were to store this data as a variable inside Planet, should it be an instance variable or a static variable? Why?
A1: As a static variable, because the variable describes the Planet objects as a group.
2: Can you assign a value to a static variable even if no objects have been created?
A2: Yes.
3: Can a static variable be accessed from within an object of the class in which it is declared?
A3: Yes.
4: The following declaration was found inside a class:
public static const double MassOfElectron = 9.0e-28;

Is it correct? Why or why not?

A4: No. A const is ...

Get C# Primer Plus now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.