General Java Questions II
constructors methods NOBODY will quote it as an authority.
How can so many people call constructors methods if they aren t.
Okay. Are they or aren t they? I holding to the definition that they are not unless it is
so common to call them that, that I will have to change.
Comments?
Answer: If you go by the JLS (Java Language Specification) and the Java API (and
you should) , then no, constructors are not methods. Consider that
Class.getMethods() returns an array of Method instances and
Class.getConstructors() returns an array of Constructor instances, and Constructor
and Method or not interchangeable (one is not derived from the other), but both
implement the Member interface. Seems to me that Java is going out of its way to
differentiate them.
Besides, the mechanics of constructors are so different from the mechanics of
methods, there seems to be no value to considering one a member of the set of the
other.
Now, as far as teaching the language goes:
Methods:
+ return types
+ called by name
+ executed multiple times
Constructors:
+ super(...) or this(...) as first instructor (often implicit)
  everything else
I very much do not like trying to lump the two concepts together, especially in
introductory courses. Conceptually they are very, very different things.
A constructor is a unique element (even if there are several). It has the name of the
class, its declaration is different, and it doesn t have the same syntax as a method. It
plays a unique role. You can call a method from a constructor, but you cannot call a
constructor from a method.
I say constructors are distinct from methods, and for students, blurring the distinction
will lead to problems.
  
by Chuck McCorvey, Chris Wolfe, Paul Lutus
 Q: Simple question: why constructor doesn t work in following example?
class Start { 
   public void Start() { 
      System.out.println("Konstruktor START"); 
   } 
} 
public class Test { 
   public static void main(String[] args) {
      Start s = new Start(); 
   } 
}
file:///F|/a_jsite/350_tips/general_java II.htm (2 of 14) [2001 07 08 11:24:53]






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java web hosting

 

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved