aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Test your knowledge Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Test your knowledge" Watch "Test your knowledge" New topic
Author

Test your knowledge

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5406
    
  13

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.

Who can add the other differences?


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
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).
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5406
    
  13

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
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.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5406
    
  13

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 )
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
8.8 Constructor Declarations
Rohit Dhodapkar
Ranch Hand

Joined: Apr 27, 2006
Posts: 38
Here's another one..

If there is no constructor in a class the JVM creates a default one. But this is not true with methods..
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5406
    
  13

that's true, but there are still 2 other ones left
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
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.


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5406
    
  13

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
A constructor cannot directly or indirectly invoke itself.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5406
    
  13

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
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

Naseem.K
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5406
    
  13

Naseem Khan,

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
Constructor can access both static and non static fileds of a calss.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5406
    
  13

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
Constructors are used to initialze instance variable when a new object is created..
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5406
    
  13

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


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."

regards

Naseem.K
[ May 17, 2006: Message edited by: Naseem Khan ]
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5406
    
  13

Naseem,

that was indeed the one i was still looking for

Congrats to you
Neha Mohit
Ranch Hand

Joined: Apr 25, 2006
Posts: 87


"instance members can't be accessed before call to super class constructors. Whereas static members can be accessed."



what is a super class constructor(ith respect to the code given by Naseem ). ?? can anybody expalin


thanx in advance
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Yeah its call to java.lang.Object(){}

regards

Naseem
[ May 17, 2006: Message edited by: Naseem Khan ]
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5406
    
  13

superclass constructor

- 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
 
jQuery in Action, 2nd edition
 
subject: Test your knowledge