The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
April 2000



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


an [constructive :-)] exercise from EBook "TIJ 2/e"

Posted by David Sills on April 13, 2000 at 3:00 PM


Your original attempt failed because in the absence of any other explicitly declared constructor, Java automatically makes a default constructor with no arguments (doesn't do anything, either). So new ATypeName() would work, since that uses this default constructor. The constructor you wanted would have to be specifically written, something like:

public class ATypeName {
String wow;

// default constructor goes here if wanted (see below)

public ATypeName(String wow)
{ = wow;

public String toString()
return wow;

Note that I have supplied a variable (after all, the parameter argument has to be held onto somewhere) and saved the parameter argument to it in the constructor. Also, I supplied a toString() method, one of the methods inherited from Object, which will be called automatically by the System.out.println(a) line of code. That method knows it's supposed to have a string parameter, and by default it will call the toString() of the object. It actually did that to get your response, since the default behavior of the toString(), inherited from Object, is to print the class name and a rather undecipherable number that represents something like a memory location, which is what you got. Now it will print out the value of the variable as a way of indicating its state, which is both clearer and one of the many possible uses for toString().

Note that this introduces another difficulty: there is now no default constructor! Since a non-default constructor has been supplied, Java does not supply a default constructor by itself, so if you want one (i.e., if you want your original working code not to break) you have to supply one:

public ATypeName()

This is actually more than had to be done: if the contents of the constructor had been empty, the String variable wow would still have been initialized to null, since this is the default behavior of instance variables (variables that belong to a class, but are declared outside a specific method).

But it's clearer this way, particularly since you can see the behavior of "this", which in this case is equivalent to calling the other constructor and passing it null for a parameter. This is perfectly legal, and is in fact the preferred way to arrange multiple constructors, particular in the common situation that you need a lot of information, and want to arrange constructors that take in less than you need but will fill in the deficits with default values. If, for instance, you wanted a default value of "Wow!" for your class, you could do that this way:

public ATypeName()

Now if you give the constructor nothing, it will save "Wow!" to the variable, but if you give it a string, it will save that string.

Hope this helps.

David Sills

> I was referred from Bruce Eckel's site "Thinking in Java 2nd ed."

> Chapter 2: Everything is an object - exercise 2 says, "Find the
> code fragments involving ATypeName and turn them into a program
> that compiles and runs."
> The code fragments are: "class ATypeName2 {}" and
> "ATypeName a = new ATypeName();"

> I did get it to compile and run with:
> class ATypeName2 {
> public static void main(String[] args) {
> ATypeName a = new ATypeName();
> System.out.println(a);
> }
> }

> //OutPut was: ATypeName@774a07df

> ...even though this is probally not what Mr. Eckel meant. But I
> did not fare well with my original code:
> class ATypeName {
> public static void main(String[] args) {
> ATypeName a = new ATypeName("Wow");
> System.out.println(a);
> }
> }

> //OutPut: Wrong number of arguments in constructor.
> // ATypeName a = new ATypeName("Wow");
> // ^
> // 1 error

> Could someone who is familiar with "TIJ" please go through
> the second code and explain in laymans terms the error and what
> I'm doing wrong? I think () is the constructor? Maybe even a few
> solutions to the exercise. I know this is an easy problem - I'm
> keeping an eye out for Thinking in Java (Sped version) LOL

> Thanks, Steve


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us