Solutions to selected exercises can be found in the electronic document The Thinking in Java Annotated Solution Guide, available for a small fee from www.BruceEckel.com.
- Create a class with a default constructor (one that takes no arguments)
that prints a message. Create an object of this class.
- Add an overloaded constructor to Exercise 1 that takes a String
argument and prints it along with your message.
- Create an array of object references of the class you created in Exercise
2, but dont actually create objects to assign into the array. When you
run the program, notice whether the initialization messages from the constructor
calls are printed.
- Complete Exercise 3 by creating objects to attach to the array of
- Create an array of String objects and assign a string to each
element. Print the array by using a for loop.
- Create a class called Dog with an overloaded bark( )
method. This method should be overloaded based on various primitive data types,
and print different types of barking, howling, etc., depending on which
overloaded version is called. Write a main( ) that calls all the
- Modify Exercise 6 so that two of the overloaded methods have two arguments
(of two different types), but in reversed order relative to each other. Verify
that this works.
- Create a class without a constructor, and then create an object of that
class in main( ) to verify that the default constructor is
- Create a class with two methods. Within the first method, call the second
method twice: the first time without using this, and the second time
- Create a class with two (overloaded) constructors. Using this, call
the second constructor inside the first one.
- Create a class with a finalize( ) method that prints a message.
In main( ), create an object of your class. Explain the behavior of
- Modify Exercise 11 so that your finalize( ) will always be
- Create a class called Tank that can be filled and emptied, and has a
termination condition that it must be empty when the object is cleaned
up. Write a finalize( ) that verifies this termination condition. In
main( ), test the possible scenarios that can occur when your
Tank is used.
- Create a class containing an int and a char that are not
initialized, and print their values to verify that Java performs default
- Create a class containing an uninitialized String reference.
Demonstrate that this reference is initialized by Java to null.
Create a class with a String field that is initialized at the point
of definition, and another one that is initialized by the constructor. What is
the difference between the two approaches?
- Create a class with a static String field that is initialized at the
point of definition, and another one that is initialized by the static
block. Add a static method that prints both fields and demonstrates
that they are both initialized before they are used.
- Create a class with a String that is initialized using
instance initialization. Describe a use for this feature (other
than the one specified in this book).
- Write a method that creates and initializes a two-dimensional array of
double. The size of the array is determined by the arguments of the
method, and the initialization values are a range determined by beginning and
ending values that are also arguments of the method. Create a second method that
will print the array generated by the first method. In main( ) test
the methods by creating and printing several different sizes of arrays.
Repeat Exercise 19 for a three-dimensional array.
- Comment the line marked (1) in ExplicitStatic.java and verify that
the static initialization clause is not called. Now uncomment one of the lines
marked (2) and verify that the static initialization clause is called.
Now uncomment the other line marked (2) and verify that static initialization
only occurs once.
href="TIJ306_001.htm"> In some of the Java literature from Sun, they instead refer to these with the awkward but descriptive name no-arg constructors. The term default constructor has been in use for many years, so I will use that.