Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Default constructor

 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To the Java Language Specification!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Randall Twede
Ranch Hand
Posts: 4482
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Wendy Gibbons
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This section and this one of the language specification might help, too.
 
Randall Twede
Ranch Hand
Posts: 4482
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I find the bit about constructor access modifiers there, however, confusing.


How so?
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic