Why the constructor name and class name must be same?
I tried with diff name but didn't work I searched on google but I didn't get proper answer
Please help me...?
Why the constructor name and class name must be same?
I tried with diff name but didn't work I searched on google but I didn't get proper answer
Please help me...?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Oct 28, 2022, 6:58 AM EDT
So when the class object is initializing, it has to call the constructor, if it is a different name, it wouldn't know what to call. that's is why it is law to keep the constructor name as class name
Amit MohantyRank #112 · 20K reputationPosted Oct 28, 2022, 7:17 AM EDT
Constructor are the first thing which gets called whenever any instantiation or object of the class is created. Constructors are named same as the class name so that the compiler knows, which method to call when an object is created.