Why do Java constructors need to have the same name as the class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why do Java constructors need to have the same name as the class?

What makes: public class Main{ public static void main(String[]args){ character f = new character(); f.saying(); } } class character { int hieght; int age; String name; void saying() { System.out.println("This is my saying"); } void saying(String s){ System.out.println(s); } } Any Different from public class Main{ public static void main(String[]args){ character f = new character(); f.character(); } } class character { int hieght; int age; String name; void character() { System.out.println("This is my saying"); } void character(String s){ System.out.println(s); } } Thanks!

25th Jul 2017, 5:15 AM
Paul Silvestri
Paul Silvestri - avatar
3 Answers
+ 6
Because this syntax doesn't require any new keyword.Aside from that, there is no good reason. Classes are the pattern from which we construct objects. And all the objects share the same structure define by the class
25th Jul 2017, 8:02 AM
Вап
+ 2
yes
25th Jul 2017, 8:22 PM
Вап
0
So constructors don't actually need to be the same name?
25th Jul 2017, 6:00 PM
Paul Silvestri
Paul Silvestri - avatar