[HD Pg 209, Sec. 8.5.2 - the-default-constructor]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

[HD Pg 209, Sec. 8.5.2 - the-default-constructor]

Post by Username987654 »

A constructor is provided by you, the programmer,
should be
A non-default constructor is provided by you, the programmer,
?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 209, Sec. 8.5.2 - the-default-constructor]

Post by admin »

I think the statement is fine in that context. The paragraph is driving the point that a constructor does not necessarily have to initialize instance fields. Since, in general, a constructor is coded by the programmer, he/she can choose what to do in the body. The para also includes the statement, " The default constructor provided by the compiler also doesn’t assign any values to the instance members."

Username987654
Posts: 95
Joined: Sat Dec 26, 2015 6:37 pm
Contact:

Re: [HD Pg 209, Sec. 8.5.2 - the-default-constructor]

Post by Username987654 »

Understood. Very well done, as always. (It's merely a suggestion to bring perhaps a tiny bit more clarity/precision(?) for that specific statement, particularly for "newbies" that may have to reread it more holistically before comprehending that statement within it's larger point.) Thanks.

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 209, Sec. 8.5.2 - the-default-constructor]

Post by admin »

sure, will add to todo list.

addy12345
Posts: 2
Joined: Sat Apr 25, 2020 12:43 pm
Contact:

Re: [HD Pg 209, Sec. 8.5.2 - the-default-constructor]

Post by addy12345 »

" if you write any constructor in a class yourself, the compiler will not provide the default constructor at all ". if there is no default constructor ,compiler will fail to compile . In that case how can we create constructors explicitly without causing compile errors?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 209, Sec. 8.5.2 - the-default-constructor]

Post by admin »

Where did you read that if there is no default constructor, compiler will fail to compile? Please read Section 8.5.2 from the beginning carefully.

The no-args constructor is required only if you want to use a no-args constructor. It is not necessary for a class to always have this constructor.

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests