B
bilsch
Add nothing; *subtract* "void".
A constructor is a special piece of code that initializes a
brand-new object. It looks superficially like a method, and you
can write the same kinds of Java statements in a constructor as
in a method, but it is not a method at all. Among the differences:
- You can call methods, but you cannot "call" constructors.
Constructors run when a `new' operation is performed, to
set up the new object. There are a few operations that
sort of look like "calls" to constructors -- one constructor
can invoke another constructor of the same class with a
this(...) construct, or a constructor of its superclass
with a super(...) -- but as you've seen you cannot just
"call" a constructor the way you'd call toString().
- Methods have names, but constructors don't. If a
constructor throws an exception and a stack trace gets
printed, you'll see something like "ClassName.<init>" as
a sort of stand-in for the name -- but you quite clearly
can't use "<init>" as the name of a method!
- Constructors have no return type, not even `void'. A method
*always* has a return type, even if it's `void'.
I think you need a Java textbook, just like everyone else did
when starting out.
Thanks for the explanation. I have some Java books. They are kind of
vague on the subject.