27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

General <strong>Java</strong> Questions II<br />

I need a little help here...I have been teaching that constructors are not methods. This<br />

is for several reasons, but mainly because JLS says "constructors are not members"<br />

and members are "classes, interfaces, fields, and methods."<br />

So, now the rest of the staff is ganging up on me and making life a little nasty. They<br />

quote Deitel and Deitel, and Core <strong>Java</strong> (which references "constructor methods") and<br />

who knows how many other books.<br />

The one we are teaching in is loaded with so many errors that even though it calls<br />

constructors methods NOBODY will quote it as an authority.<br />

How can so many people call constructors methods if they aren't.<br />

Okay. Are they or aren't they? I holding to the definition that they are not unless it is<br />

so common to call them that, that I will have to change.<br />

Comments?<br />

Answer: If you go by the JLS (<strong>Java</strong> Language Specification) and the <strong>Java</strong> API (and<br />

you should) , then no, constructors are not methods. Consider that<br />

Class.getMethods() returns an array of Method instances and<br />

Class.getConstructors() returns an array of Constructor instances, and Constructor<br />

and Method or not interchangeable (one is not derived from the other), but both<br />

implement the Member interface. Seems to me that <strong>Java</strong> is going out of its way to<br />

differentiate them.<br />

Besides, the mechanics of constructors are so different from the mechanics of<br />

methods, there seems to be no value to considering one a member of the set of the<br />

other.<br />

Now, as far as teaching the language goes:<br />

Methods:<br />

+ return types<br />

+ called by name<br />

+ executed multiple times<br />

Constructors:<br />

+ super(...) or this(...) as first instructor (often implicit)<br />

- everything else<br />

I very much do not like trying to lump the two concepts together, especially in<br />

introductory courses. Conceptually they are very, very different things.<br />

A constructor is a unique element (even if there are several). It has the name of the<br />

class, its declaration is different, and it doesn't have the same syntax as a method. It<br />

plays a unique role. You can call a method from a constructor, but you cannot call a<br />

constructor from a method.<br />

I say constructors are distinct from methods, and for students, blurring the distinction<br />

will lead to problems.<br />

-by<br />

Chuck McCorvey, Chris Wolfe, Paul Lutus<br />

Q: Simple question: why constructor doesn't work in following example?<br />

class Start {<br />

public void Start() {<br />

System.out.println("Konstruktor START");<br />

}<br />

}<br />

file:///F|/350_t/350_tips/general_java-II.htm (2 of 13) [2002-02-27 21:18:24]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!