+ 10
Why i am not getting an error while having same name for class and method in java??
I tried this and the code compiled successfully!! How's this possible in Java? whereas in c# I got an error!! class MyClass { static void MyClass () { System.out.println("2"); } public static void main(String[ ] args) { MyClass.MyClass (); } }
16 ответов
+ 9
actually u made a constructor..
+ 8
@Meharban: you are wrong, it is not a constructor, it is a static class method.
@Anusha: in Java a method can has the same name as class has.
+ 7
OMG Meharban, don't speak stupid please :(
Do you see "new" in Anusha's question? Do you see "static void" before MyClass? Do you know what do they mean?
+ 7
@tamás barta
thank you for your answer!!
+ 5
@ram Kumar
yeah..I know I am violating naming convention.
but still I just wanted to know whether it works!!And it did🤓
+ 5
that's okey🤓@Meharban Singh
+ 4
of course, it works.. but, don't you think that you're violating Java's name convention..?? only constructor can have same same as class... mean, a upper case starting letter n method to be an lower case..?
constructor is non-static and implicitly void return type. the one you created is a separate method which could face no problem, mostly.. but still some compilers warn saying that name is same as a constructors.
+ 4
oops, sorry I messed up :-( @anusha @ramKumar
@Tamas
+ 4
thanks 😓 @Anusha.
By the way you seem to be Indian.Am i true?
+ 3
hi
+ 3
how r u
+ 2
oo
+ 2
I
+ 1
hi
0
because java allow to have same name of class and it's member method but c# not
0
As long as the complete signatures are different Java allows same name for different methods/constructors.