| Author |
Default constructor
|
Sudhanshu Mishra
Ranch Hand
Joined: May 28, 2011
Posts: 158
|
|
Hi all,
I have learnt that the instance variables get their defualt values.Well,who initilalizes them?Is it the default constructor that is provided by Java?What is the real task of default constructor?
|
Sudhanshu Mishra
OCPJP 6
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 26720
|
|
|
To the Java Language Specification!
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 2686
|
|
Sudhanshu Mishra wrote:Well,who initilalizes them?
I would imagine the compiler puts bytecode in the class to set default values if there is no explicit code to initialise them.
Edit: but following Campbell's advice is the way to be sure.
Sudhanshu Mishra wrote:Is it the default constructor that is provided by Java?What is the real task of default constructor?
No. The only purpose of the default constructor is to call the no argument constructor of the super class.
|
Joanne
|
 |
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 3905
|
|
anyone feel free to correct me if i am wrong. the instance variables get their default values from the compiler. the default constructor is a convenience for us so we don't have to write a no arguments constructor unless we also write one that takes arguments.
ha ha joanne beat me by 19 seconds
|
I never took notes in college. That's how I got a 4.0 the first 2 years, and a 3.5 the second two years.
|
 |
Wendy Gibbons
Bartender
Joined: Oct 21, 2008
Posts: 774
|
|
Ok so the default constructor is the one without any parameters. You don't HAVE to code one it just keeps calling the super constructor till it finds one. It will find one at Object.
You can make a default constructor if you need to do stuff at class instansiation time, or you can make ones with parameters.
If you don't include a call to a super constructor in YOUR constructor an implict one to the default super constructor happens.
f you create a constructor with parameters. no default constructor exists.
I think the JVM sets the default values on instance variables, as the constructor on Object knows nothing about your variables.
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 2686
|
|
Wendy Gibbons wrote:Ok so the default constructor is the one without any parameters. You don't HAVE to code one it just keeps calling the super constructor till it finds one. It will find one at Object.
You can make a default constructor if you need to do stuff at class instansiation time, or you can make ones with parameters.
If you don't include a call to a super constructor in YOUR constructor an implict one to the default super constructor happens.
f you create a constructor with parameters. no default constructor exists.
I think the JVM sets the default values on instance variables, as the constructor on Object knows nothing about your variables.
No. The default constructor is the one the compiler adds automatically if you don't include a constructor in your code.
If you explicitly put a constructor in your code that takes no arguments, then that is a no argument constructor.
A subtle but important difference.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 26720
|
|
|
This section and this one of the language specification might help, too.
|
 |
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 3905
|
|
|
after thinking a little i agree it is probably the JVM not the compiler that sets the default values of the member variables as this might be platform dependent
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 26720
|
|
|
I am not convinced it’s in the bytecode, Joanne.If you look around, you find some places where they say it is poor style to use a default constructor. I find the bit about constructor access modifiers there, however, confusing.
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 2686
|
|
Campbell Ritchie wrote:I am not convinced it’s in the bytecode, Joanne.
'twas just a guess. I did edit my post to suggest the OP followed your advice to read the JLS.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 3141
|
|
Campbell Ritchie wrote:I find the bit about constructor access modifiers there, however, confusing.
How so?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 26720
|
|
I couldn’t see why you have to write a public constructor just because the default constructor is public . . .
. . . I think I have just understood the bit about access modifiers
|
 |
 |
|
|
subject: Default constructor
|
|
|