Why constructor name same as class name | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 3

Why constructor name same as class name

7th Oct 2017, 6:06 AM
Kanchan Chavan
Kanchan Chavan - avatar
5 Respostas
+ 12
Constructor creates an instance of class. The names are same so that it can be unambigously identified as theĀ constructor. and how its get invoked implicitly when we create object of thatĀ class. It is invoked by the compiler because it has already been unambiguously identified because of its naming sheme.
7th Oct 2017, 6:23 AM
Apoorva Shenoy Nayak
Apoorva Shenoy Nayak - avatar
+ 6
If it wouldn't be like this it would be interpreted like method
7th Oct 2017, 6:09 AM
Nerphist
Nerphist - avatar
+ 4
To avoid ambiguity.
7th Oct 2017, 7:20 AM
Anuj Khetan
Anuj Khetan - avatar
+ 1
There is no reason. It's just what the inventors decided. In other languages it's different. See the Ruby course for example.
7th Oct 2017, 1:56 PM
1of3
1of3 - avatar