• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Confusion about the default constructor

 
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?
 
Ranch Hand
Posts: 10191
3
Mac PPC Eclipse IDE 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.
 
Enthuware Software Support
Posts: 4364
39
  • 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

 
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
Posts: 4364
39
  • 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.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • 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!
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!