aspose file tools*
The moose likes Java in General and the fly likes Default constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Default constructor" Watch "Default constructor" New topic
Author

Default constructor

Sudhanshu Mishra
Ranch Hand

Joined: May 28, 2011
Posts: 207

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

Joined: Oct 13, 2005
Posts: 36579
    
  16
To the Java Language Specification!
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3169
    
  10
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: 4339
    
    2

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


SCJP
Visit my download page
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1106

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: 3169
    
  10
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: 36579
    
  16
This section and this one of the language specification might help, too.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

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: 36579
    
  16
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: 3169
    
  10
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: 6109
    
    6

Campbell Ritchie wrote:I find the bit about constructor access modifiers there, however, confusing.


How so?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36579
    
  16
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Default constructor
 
Similar Threads
Constructor of a Servlet ?
this(Color.Red) ?
Which statement is true for default Constructor?
Spring: Hello World Does Not Work
Implicit Constructor