Blakes 21 Days Chapter 03 Document


Day 3, Working with Objects

Creating New Objects

Using new

Listing 3.1

Listing 3.1 (screenshot from NetBeans) goes here

152

Source & Output

153

How Objects are Constucted

A Note on Memory Management

Using Class and Instance Variables

Getting Values

Setting Values

Listing 3.2

Listing 3.2 (screenshot from NetBeans) goes here

Figure 3.2 (screenshot from NetBeans Output pane) goes here

Class Variables

Calling Methods


Listing 3.3

Figure 3.3 Goes Here

Formatting Strings

Nested Method Calls

Class Methods

References to Objects

Listing 3.4

Listing 3.4 (screenshot from NetBeans) goes here

Figure 3.4 displays the Ouput pane

Figure 3.5 goes here

Casting Objects and Primitive Types

Casting Primitive Types

Casting Objects

Converting Primitive Types to Objects and Vice Versa

Comparing Object Values and Classes

Comparing Objects

Listing 3.5

Listing 3.5 (screenshot from NetBeans) goes here

Figure 3.6 (screenshot from NetBeans Output pane) goes here

Determining the Class of an Object

Summary

Q & A

Quiz - Questions

  1. What operator do you use to call an object's constructor and create a new object ?
    1. +
    2. new
    3. instanceof
  2. What kind of methods apply to all objects of a class rather than an individual object ?
    1. Universal methods
    2. Instance methods
    3. Class methods
  3. If you have a program with objects named obj1 and obj2, what happens when you use the statement obj2 = obj1 ?
    1. The instance variables in obj2 are given the same values as obj1.
    2. obj2 and obj1 are considered to be the same object.
    3. Neither A nor B.

Answers

  1. B. The new operator is followed by a call to the object's constructor.
  2. C. Class methods can be called without creating an object of that class.
  3. B. The = operator does not copy values from one object to another. Instead, it makes both variables refer to the same object.

Certification Practice

  1. What is the value of the j variable at the time it is displayed inside the setValue() method ?
    1. 42
    2. 40
    3. 21
    4. 20

Exercise



Reserve