Page 1 of 1

Default Constructor Errata

Posted: Thu Jun 11, 2020 9:04 am
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

Re: Default Constructor Errata

Posted: Thu Jun 11, 2020 9:24 am
by admin
The line of code inside the main method as given in the book is:
Account a = new Account();

Re: Default Constructor Errata

Posted: Thu Jun 11, 2020 10:07 am
by gauravnigam
Yes, you are right.