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);
}
}
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
Gaurav