Why does the JVM initialise the data fields with default values if no constructors are defined in any class? What is the need for such a procedure of calling the default constructor?

Recommended Answers

All 2 Replies

> Why does the JVM initialise the data fields with default values
AFAIK, design decision by the language implementors. Also makes sense since I wouldn't want the members of my newly created instance to have a value of 'undefined'.

> What is the need for such a procedure of calling the default constructor?
Instances in Java are created by the invocation of a series of constructors. The first line of each constructor is a call to it's superclass's constructor except in the case of 'Object' class. If Java doesn't provide a default constructor, it would become necessary for the programmer to specify a default constructor for each class so as not to break the constructor chaining mechanism and thereby the language specification.

commented: Thank you! +2

Thank you :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.