Default Constructor Errata

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

Moderator: admin

Post Reply
gauravnigam
Posts: 2
Joined: Wed May 27, 2020 8:46 am
Contact:

Default Constructor Errata

Post by gauravnigam »

Hi Admin,

I have created the following class with only non default constructor and ran the java class. It works all fine. As per the book section 10.4.2 of JAVA SE 11 Programmer 1 Exam Fundamentals 1Z0-815. It should give me the compile time error. Can you please help me to understand is that errata issue or I am doing some thing different.

Here is block of code:

Code: Select all

public class Account {

    private final int id;

    public Account(int id){
        this.id = id;
    }

    public static void main(String[] args) {
        Account account = new Account(1);
    }
}
Excerpt from the book :

Code: Select all

I have provided a constructor explicitly in the above class. Can you can guess what will happen if I try to compile
this class? It will not compile. The compiler will complain that Account class does not have a constructor that
takes no arguments. What happened to the default constructor, you ask? Well, since this class provides a
constructor explicitly, the compiler feels no need to add one on its own
Thanks,
Gaurav

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

Re: Default Constructor Errata

Post by admin »

The line of code inside the main method as given in the book is:
Account a = new Account();

gauravnigam
Posts: 2
Joined: Wed May 27, 2020 8:46 am
Contact:

Re: Default Constructor Errata

Post by gauravnigam »

Yes, you are right.

Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests