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!

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more