+ 1

java

I have found this code https://code.sololearn.com/cA1a1a248a16 and I don't really understand why when I create the 'c' variable using new the constructor is not called? It is called only if I use c.First_C();.

21st Jun 2021, 3:41 PM
tibi
tibi - avatar
4 ответов
+ 4
Because you have not defined a constructor for the class. public void First_C() { System.out.println("Constructor"); } This is a method and not a constructor. Constructor doesn't have a return type.
21st Jun 2021, 6:24 PM
Avinesh
Avinesh - avatar
+ 1
The declaration public void First_C() specifies a type (void) and therefore it is not a valid constructor declaration. A constructor does not declare type. It should be: public First_C()
21st Jun 2021, 6:28 PM
Brian
Brian - avatar
+ 1
Oh I haven't seen that is public void.
21st Jun 2021, 7:34 PM
tibi
tibi - avatar
0
Ya I also
24th Jun 2021, 4:57 AM
Naman Verma
Naman Verma - avatar