0

java file must have the same name as java file?? true or false?? this is the question and answer in sololearn is true......

but is it necessary u can save .java file with any name and compile in commandline successfully using javac compiler...then u can execute it using class name in which main method is present......so it is not must to have java file name as class file name

22nd Jul 2017, 3:21 PM
Abhishek Gupta®
Abhishek Gupta® - avatar
19 odpowiedzi
+ 3
hope this is better explanation https://code.sololearn.com/ch3CMVGBoJvd/?ref=app
22nd Jul 2017, 4:01 PM
Abhishek Gupta®
Abhishek Gupta® - avatar
+ 3
thanks martin u cleared one of my mistake now i know if use public class than i must have to save java file name as class name
22nd Jul 2017, 4:30 PM
Abhishek Gupta®
Abhishek Gupta® - avatar
+ 2
in question i am talking about .java file not about .class file read it...
22nd Jul 2017, 3:39 PM
Abhishek Gupta®
Abhishek Gupta® - avatar
+ 2
ok sorry but ur question was not clear plz can u clearly ask ur question i'll try to answer it if know.
22nd Jul 2017, 3:40 PM
shobhit
shobhit - avatar
+ 2
yeah it is not necessary but then i think u would end up compiling that file with the name u provided resulting in a output of. class file with same name u provided and then it will give error.
22nd Jul 2017, 3:44 PM
shobhit
shobhit - avatar
+ 2
but at compiling time it is not necessary
22nd Jul 2017, 3:47 PM
Abhishek Gupta®
Abhishek Gupta® - avatar
+ 2
i have one more question on arrays how to find contiguous subarrays of an array.... i posted this question and described it properly butno one answered......i want to know algorithm use for that
22nd Jul 2017, 5:07 PM
Abhishek Gupta®
Abhishek Gupta® - avatar
+ 1
i have tried it u can try it
22nd Jul 2017, 3:32 PM
Abhishek Gupta®
Abhishek Gupta® - avatar
+ 1
if sololearn allow pictures i will post it
22nd Jul 2017, 3:35 PM
Abhishek Gupta®
Abhishek Gupta® - avatar
+ 1
my question is sololearn challenge question say .java file must have same name as class name.......but it is not necessary
22nd Jul 2017, 3:42 PM
Abhishek Gupta®
Abhishek Gupta® - avatar
+ 1
i lose challenge because of wrong answer of sololearn
22nd Jul 2017, 3:43 PM
Abhishek Gupta®
Abhishek Gupta® - avatar
+ 1
yes martin u are right but question is not about declaring public
22nd Jul 2017, 3:45 PM
Abhishek Gupta®
Abhishek Gupta® - avatar
+ 1
yes u are right shobit at execution time class in which main method consist of must be give
22nd Jul 2017, 3:46 PM
Abhishek Gupta®
Abhishek Gupta® - avatar
+ 1
If you declare your class public, the filename must match the class name. Otherwise this is not mandatory
22nd Jul 2017, 7:03 PM
marit vandijk
0
just try it once it will not going to run.
22nd Jul 2017, 3:30 PM
shobhit
shobhit - avatar
0
it ran or not?
22nd Jul 2017, 3:33 PM
shobhit
shobhit - avatar
0
i have played a lot with java and if. class file doesnt match with that of class it simply gives error.
22nd Jul 2017, 3:37 PM
shobhit
shobhit - avatar
0
look suppose ur. java file is not same as the class name then ur code will compile but it will result in a. class file with the name that u have written and will not be the name of class which has main(), at time of execution jvm try to access that class whose name is something else but as u provided different it will end up in not finding it giving error.
22nd Jul 2017, 3:52 PM
shobhit
shobhit - avatar
0
but still, it can create confusion while running program.
22nd Jul 2017, 4:11 PM
shobhit
shobhit - avatar