Page 403 - Introduction to Programming with Java: A Problem Solving Approach
P. 403
Review Question Solutions
1. True.
2. You should declare a variable to be a class variable as opposed to an instance variable if the variable holds data that is associated with the class as a whole. You should use class variables to describe properties of a class’s objects that need to be shared by all of the objects.
3. The default values for class variables are the same as they are for instance variables of the same type. Here are the default values:
integer types get 0
floating point types get 0.0 boolean types get false reference types get null
4. Because there’s no static modifier, getAge is an instance method. The Mouse4.getAge() call uses a Mouse4 dot prefix. It’s illegal to use a class name (Mouse4) as a prefix for an instance method call. To call an instance method, you need to use a reference variable dot prefix.
5. Member access:
a) False. You cannot use this in a class method.
b) True. You can always use the class name as a class method prefix.
c) True, if the main method is “merged” into the same class as the other method.
False, if the other method is in a different class.
Including the class name prefix allows you to move the main method to another class later.
Apago PDF Enhancer
6. True. You can access a class member from an instance method and also from a constructor—just prefix the class member with the class name.
7. False. You can access an instance member from a class method only if you prefix the method name with a reference to a particular object.
8. You should make a method a class method:
a) If you have a method that uses class variables and/or calls class methods, then it’s a good candidate for
being a class method.
b) If you might need to call a method even when there are no objects from the method’s class, then you
should make it a class method.
c) The main method has to be a class method. If a main method uses helper methods that don’t involve
instance members, then the helper methods should be class methods.
d) If you have a general-purpose method that stands on its own, make it a class method.
9. The keyword final converts a variable into a constant.
10. True. Use static to make a constant be the same for all objects.
11. False. A class constant should normally be initialized as part of its declaration. If it is assigned a value later on, including within a constructor, it generates a compilation error.
12. Minimum passing score declaration:
private static final double MIN_PASSING_SCORE = 59.5;
13. False. A utility class’s members should normally use the public and static modifiers.
Review Question Solutions 369