DEV Community

Cover image for Tech notes 01 - Two Constructors in Java
Omar
Omar

Posted on

Tech notes 01 - Two Constructors in Java

I'm studying OOP using the Java How to Program Book. In exercise 4.19 - (Sales Commission Calculator) when I try to solve it. If we have two constructorswhen you call oneof them, JVMwill ignore the other one.
Check the pictures to know more.

1. case 1:

Image description

In the default constructor I just initialized

this.rate = 0.09;
Enter fullscreen mode Exit fullscreen mode

variable, but I didn't use this constructorby the way!

2.case 2:

Image description

I did initialize

this.rate = 0.09;
Enter fullscreen mode Exit fullscreen mode

with a non-zero value, if I didn't give it a value, the compiler will equal it to zero **
**Note: this is an opinion, I'm a student, if you have another answer for this case, please tell me! Thanks!

Top comments (0)