"As usual, constructors are different from other kinds of methods. This is also true when polymorphism is involved. Even though constructors are not polymorphic (they�re actually static methods, but the static declaration is implicit)it�s important to understand the way constructors work in complex hierarchies and with polymorphism."
3rd Edition,Chapter 7(Polymorphism),page 315
Now, i just want to know what does the author mean by calling constructors,static methods,because we don't have static constructors in Java?Thanks a lot..
I think what he means is that they are something which is called from the class rather than an instance, but I suspect calling constructors "static" and calling them "methods" are both inaccurate. I was told off once on this site for calling constructors "static" because of that quote.
Joined: Jun 28, 2008
Thanks for the reply,but still i m in doubt.I have read the review of Thinking in Java,written by kathie Sierra herself.I think of this book very highly,and any comment in this book should'nt be taken lightly...
I think this is more a problem for Wittgenstein than for programmers. I feel fairly certain that what the author means is that constructors (things very much like methods though maybe technically not) belong to classes and not objects (very much like static methods do though maybe they're technically not static). The problem here is just in terminology which from a learning perspective is pretty useless. Technically in this case means whatever the JLS writers wanted to call it. They name somethings well and somethings less well but you'll always run into conceptual problems.
Java's answer to static constructors is the static initializer. It's called called a static initializer because its used to initialize static variables when a class is loaded. That's not very much like what the author means by saying that the constructor is static but you can see why he used that word. He meant it to be conceptually helpful and in this case it was not.
I have a lot of respect for Eckel and his Thinking in Java books, but I think he's occasionally a bit casual with his terms. (Another example is his use of "inner classes," which is not consistent with the Java Language Specification.)
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
Originally posted by Max White: ... Now is it even possible for someone like Bruce Eckel to write this casually?
I think Eckel's style is to illustrate concepts in different ways, and his emphasis is more on getting you to understand how things work rather than using ultra-precise terminology that's always in accordance with the Java Language Specification.
In the 3rd edition of TIJ, he introduces the concept by saying...
You can imagine creating a method called initialize() for every class you write. The name is a hint that it should be called before using the object.
He then expands on this concept of an initializer method by saying...
In Java, the class designer can guarantee initialization of every object by providing a special method called a constructor.
Maybe the key is what the word "special" entails, but I'm not sure he really defines that. [ August 25, 2008: Message edited by: marc weber ]
Joined: Jun 28, 2008
Thanks for the explanation,marc.I m getting to understand things.
subject: Thinking in Java !! It really makes me think