Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Test your knowledge

 
Roel De Nijs
Sheriff
Posts: 10223
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10223
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10223
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
8.8 Constructor Declarations
 
Rohit Dhodapkar
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10223
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's true, but there are still 2 other ones left
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Roel De Nijs
Sheriff
Posts: 10223
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor cannot directly or indirectly invoke itself.
 
Roel De Nijs
Sheriff
Posts: 10223
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10223
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructor can access both static and non static fileds of a calss.
 
Roel De Nijs
Sheriff
Posts: 10223
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors are used to initialze instance variable when a new object is created..
 
Roel De Nijs
Sheriff
Posts: 10223
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Sheriff
Posts: 10223
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naseem,

that was indeed the one i was still looking for

Congrats to you
 
Neha Mohit
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


"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
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah its call to java.lang.Object(){}

regards

Naseem
[ May 17, 2006: Message edited by: Naseem Khan ]
 
Roel De Nijs
Sheriff
Posts: 10223
129
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic