Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusion about the default constructor

 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is default constructor responsible for assigning default vales of the instance variables?

In enthuware/Hanumanth deshmukh's book they say no.

In Mala gupta's book it says yes.

Please can some body clarify this?
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is sort of right in a sense that the default constructor initializes the instance variables to its default values.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3819
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe Harry wrote:It is sort of right in a sense that the default constructor initializes the instance variables to its default values.

No, it doesn't.
As per Section 12.5 of JLS 7:

Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden (§8.3).

If there is not sufficient space available to allocate memory for the object, then creation of the class instance completes abruptly with an OutOfMemoryError. Otherwise, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values (§4.12.5).

Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:


HTH,
Paul.
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there is not sufficient space available to allocate memory for the object, then creation of the class instance completes abruptly with an OutOfMemoryError. Otherwise, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values (§4.12.5).


So Paul are you saying default constructor is responsible for initializing its and all of its superclass's instance variables to its default values? (when memory is sufficient).

Also take a look at this link below, about the default constructor

http://stackoverflow.com/questions/26943873/is-a-default-constructor-responsible-for-initializing-members-to-default-values

 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would argue that it can't be responsible, because if you replace the default constructor with your own constructor the default one is never called...yet the instance variables are still initialised.

As Paul's quote from the JLS shows, the initialisation happens before the constructor is called.
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3819
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:

So Paul are you saying default constructor is responsible for initializing its and all of its superclass's instance variables to its default values?


No, I am saying just the opposite. Nothing to do with memory. You might want to read the quote again.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:In Mala gupta's book it says yes.

Can you provide the location of that statement (page number, section, paragraph,...)? So the author can be informed about this errata item.
 
Prathima gaitonde
Ranch Hand
Posts: 130
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Sure.

The page no. 141 : constructors of a class. Chapter 3 Methods and encapsulation. 1st paragraph starts with "In this case,... " last 3 lines.

"In the absence of user-defined constructor, Java inserts a default constructor. This constructor doesn't accept any method arguments. It calls the constructor of the super (parent) class and assigns default values to all the instance variables." .
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prathima gaitonde wrote:The page no. 141 : constructors of a class. Chapter 3 Methods and encapsulation. 1st paragraph starts with "In this case,... " last 3 lines.

"In the absence of user-defined constructor, Java inserts a default constructor. This constructor doesn't accept any method arguments. It calls the constructor of the super (parent) class and assigns default values to all the instance variables." .

I verified in my e-book of the study guide. That's indeed an errata item. I will pass this on to the author!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic