There are (at least) 7 differences between a constructor and a method. The 2 most obvious and easy ones, i 'll give myself: - a constructor can't have a return type - a constructor 's name must exactly match the class name in which it's declared.
1)There are (at least) 7 differences between a constructor and a method. The 2 most obvious and easy ones, i 'll give myself: - a constructor can't have a return type - a constructor's name must exactly match the class name in which it's declared. - constructor's are not inherited where as methods are. so constructor's can not be overriden. - constructor's can not be abstract where as methods can be. constructor's can not be incomplete where as methods can be. - constructor's can not be static where as methods can be. - constructor's can not be final where as methods can be. - constructor's can be called only from another constructor(In superclass or subclass constructor).
Originally posted by Girish Nagaraj: 1)There are (at least) 7 differences between a constructor and a method. The 2 most obvious and easy ones, i 'll give myself: - a constructor can't have a return type - a constructor's name must exactly match the class name in which it's declared. - constructor's are not inherited where as methods are. so constructor's can not be overriden. - constructor's can not be abstract where as methods can be. constructor's can not be incomplete where as methods can be. - constructor's can not be static where as methods can be. - constructor's can not be final where as methods can be. - constructor's can be called only from another constructor(In superclass or subclass constructor).
nice one
but "constructor not be marked abstract, static, final" is seen as 1 difference
and what do you mean with "incomplete"
so we've got already:
1. a constructor can't have a return type 2. a constructor's name must exactly match the class name in which it's declared. 3. a constructor is not inherited where as methods are, so constructor's can not be overriden. 4. a constructor can not be abstract, static or final where as methods can be. 5. a constructor can be called only from another constructor(In superclass or subclass constructor). [ May 15, 2006: Message edited by: Roel De Nijs ]
Girish Nagaraj
Ranch Hand
Joined: Apr 19, 2006
Posts: 153
posted
0
6)Constructor calls superclass constructor implicitly. Where as such a thing is not applicable for methods. 7)Constructor can be called only once(ie., When the instance is created), Where as method can be called on the same instance any number of times.
Originally posted by Girish Nagaraj: 6)Constructor calls superclass constructor implicitly. Where as such a thing is not applicable for methods. 7)Constructor can be called only once(ie., When the instance is created), Where as method can be called on the same instance any number of times.
6 --> that's not completely true, but your on the right track. give it another thought and you'll be able to modify it to be 100% correct. 7 --> that's also a good one (i didn't think about that one, so we are looking for 8 now )
that's true, but there are still 2 other ones left
Naseem Khan
Ranch Hand
Joined: Apr 25, 2005
Posts: 809
posted
0
These r few more...
1. Constructors can't be marked as synchronized or native. 2. Constructors are not member of class. 3. In constructors always first line is super() if u won't explicitly mention this() or some other constructor. In classes Grandparent, Parent and Child, first constructor of Grandparent is called, then Parent, then Child. Whereas in method the one which u specify will be called.
Originally posted by Naseem Khan: These r few more...
1. Constructors can't be marked as synchronized or native. 2. Constructors are not member of class. 3. In constructors always first line is super() if u won't explicitly mention this() or some other constructor. In classes Grandparent, Parent and Child, first constructor of Grandparent is called, then Parent, then Child. Whereas in method the one which u specify will be called.
1 --> you forgot one: strictfp (==> 4. a constructor can not be abstract, static, native, synchronized, strictfp or final where as methods can be.) 2 --> that's true 3 --> that was also one of the 7 i was looking for: first line in a constructor is always a call to this() or super()
so there is still one difference missing that i 've thought of and that isn't mentioned so far
Melle Lee
Greenhorn
Joined: Nov 17, 2005
Posts: 12
posted
0
A constructor cannot directly or indirectly invoke itself.
Originally posted by Melle Lee: A constructor cannot directly or indirectly invoke itself.
that's also true, but still there is another difference that isn't mentioned in this thread and is also mentioned in the K&B book, so it's not an invention of my sick mind :roll:
Naseem Khan
Ranch Hand
Joined: Apr 25, 2005
Posts: 809
posted
0
Recursive call is not possible in constructors. Most of the compiler will flag this out as an error
I compiled this code on j2sdk 1.5 and I got compilation error.
Whereas in method u won't get any compilation error in recursive call.
In code 2, at runtime Stack explodes. But it compiles correctly. This could be one more diff b/w two
I discovered that one also yesterday. i didn't know that, but it's true: my compiler flags this also as an error. (in the k&b book it's not mentioned as a compiler error, but as a stack that will explode some time)
but we are still looking after 1 difference, i'll throw a little hint into the game: it has something to do with the instance variables...
vishnu hiranamayee
Greenhorn
Joined: Feb 23, 2006
Posts: 17
posted
0
Constructor can access both static and non static fileds of a calss.
Originally posted by vishnu hiranamayee: Constructor can access both static and non static fileds of a calss.
no, that's not correct (especially for the non static fields is there a certain clause that must be fulfilled before constructor can access a instance field and that clause isn't necessary for methods)
Rohit Dhodapkar
Ranch Hand
Joined: Apr 27, 2006
Posts: 38
posted
0
Constructors are used to initialze instance variable when a new object is created..
Originally posted by Rohit Dhodapkar: Constructors are used to initialze instance variable when a new object is created..
it's not good design, but you could use a no-arg constructor and call an init-method to initialize your instance variables, so that's not a difference
Naseem Khan
Ranch Hand
Joined: Apr 25, 2005
Posts: 809
posted
0
Above code will not compile correctly showing compilation error output...
"Can't refer i before superclass constructor has been called". But if same i is static then this code will compile and run correctly.
This is one of 7 u will be surely looking for "instance members can't be accessed before call to super class constructors. Whereas static members can be accessed."
- the mother class of all classes = class Object ==> every class extends the Object class, even when you don't mention it explicitly
e.g. class A {} (class A is a subclass of Object) class B extends class A {} class C extends class B {} class D { public static void main(String[] args) { C c = new C(); // line 1 }
what happens on line 1: - constructor of C is called - constructor of C first calls constructor of its superclass, so constructor class B is called - constructor of B first calls constructor of its superclass, so constructor class A is called - constructor of A first calls constructor of its superclass, so constructor class Object is called - constructor of Object completes - constructor of A completes - constructor of B completes - constructor of C completes
so there are a lot of things that happen behind the scenes