Originally posted by Andrew Nomos:
A constructor is called during object instantiation and cannot be invoked explicitly.
A method represents object behaviour and can be invoked at any time.
Well...
If you have a constructor declared...
public MyClass(
String s, int i)
you can "explicitly" invoke it with...
MyClass myObj = new MyClass("Andrew", 42);
Granted, you're actually expilicitly invoking the "new", but there is an obvious correspondence between the constructor signature and the code written to access it.
Other difference:
- Constructor chaining. A call to super() or super(some args) must be the first statement in a constructor if one appears at all. If the first executable statement isn't a call to one of the superclass's constructors, then a call to super() is automatically added.
- Explicit "return type". Calling "new Component()" will definitely give you a Component. However if you have a method with a declared return type of Component, it is free to return a Component or an instance of any of its subclasses.
There's probably more.
Ryan