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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.