Why this program giving me output instead of complie time error? | Sololearn: Learn to code for FREE!

+13

Why this program giving me output instead of complie time error?

As per my knowledge we can declare ONLY ONE top level public class in a Java file. If we are trying to create more than one public class in a java file then we will definitely get a compile time error. But In this program i have created three public class but I'm not getting any error. I'm not getting why this happening in Sololearn. Because it's should be throwing an compile time error instead of output. https://code.sololearn.com/cGlCd8mvkfgH/?ref=app

7/26/2020 7:29:53 PM

P∆WAN M∆URY∆

32 Answers

New Answer

+10

P∆WAN M∆URY∆ Maybe Sololearn compiler is different. Maybe they have written something different which can make separate file for each class. I tried on other online compiler but it gives error which will obviously give error because we can make only one top level class.

+6

Preity this thread not covering my answer, thanks for your answer :)

+5

https://www.sololearn.com/post/354464/?ref=app Julia Denise Roßberg Gaurav Agrawal Sumit Programmer😎😎 S.Adil 🇦🇫 Tashi N AJ #L1G3 Michal Straka 🇨🇿

+5

Yeah.. It is strange. It should be a compile time error. Maybe, jvm(or kind of it) in sololearn code playground somehow creates several .class files for each public class. And then all these files executes...

+5

This can be an reasonable answer which present in this thread by Martin. https://www.sololearn.com/Discuss/1020889/?ref=app it's sololearn system which works like this all files are send in one go and then .class file is generated at server and result is given back

+5

Jayakrishna🇮🇳 In my question. I'm not talking about any order of execution or any entry point of the program. Because if you will compile the same code on your machine you will get compile time error. But here I'm not getting any error. And the code complied successfully and getting output. So, I am curious to know how the sololearn compiler working. And how it's deal with single java file with multiple public classes.

+5

Jayakrishna🇮🇳 your this program seriously so strange. https://code.sololearn.com/cQ05vRLClI4z/?ref=app It is showing error just because of class "word". 🙄. I can guess it will be a BUG (Sololearn Java in complier). @all i read some of the article. In all the articles they mentioned we can define only one top level public class as we all know. But HERE....! And also couldn't found anything new changes related to this in newer version.

+4

Most of the other online compiler can't handle multiple classes. If you put every class in a single file the code will work also with a normal IDE. But I don't know if sololearn really creates files for each class or if it just simulates the effect. In any case, it is not a bug, but certainly wanted so that you can write several classes. The codeplayground would otherwise be very unattractive. Btw: It does not work for enum classes (which needs also separate files).

+4

Jayakrishna🇮🇳 I guess only sololearn could answer. I just can guess that it has to do with the way how sololearn deals with multiple classes.

+4

Jayakrishna🇮🇳 yeah i also went checked this link while my analysis. And i also didn't found the exact cause of this upto now. 😊

+3

A simple script can easily strip public keyword from all classes but one, at the server side or each classes can be extracted and wtitten to separate files before execution.

+3

Jayakrishna🇮🇳 It is a bug. For some reasons the code playground is not able to difference between a string "class" and the keyword class. If you remove "class" from all print statements it works.

+3

~ swim ~ Weird. I always thought just the word "class" causes the problem but did not know that it works for some print statements.

+3

~ swim ~ I actually, at that time I posted, I got different different answer about alphabetical order but I forgot where I missed that to mention, may I tested wrong also.. I forgot about that... But it seems correct alphabetical order. And in this, recent program, It don't have print in next line.. But what Denise Roßberg said is working.. But how it is.. As same program without all default access specifiers is working fine...2nd program.. What compiler deal with printf as it's just strings.. https://code.sololearn.com/cQ05vRLClI4z/?ref=app https://code.sololearn.com/chFbAY52rHF5/?ref=app

+3

But my 2nd program not getting trouble by class.. Same program without public class.. ~ swim ~ Denise Roßberg you guessed right. But i confused, what is there relation with output statements.. Since those are different strings.. We can use anything in strings in java... Ok. Thanks for the replies to both of you.. It may disturb the questioner, and you, if ask more here.... Thank you.......

+3

Jayakrishna🇮🇳 Issue is there only when you have more than one public classes

+3

~ swim ~ then @Pawan questioner comes to ask you, "why his above Program is fine"... 😂?

+3

P∆WAN M∆URY∆ this below link discussion telling that, it is allowed from java11 and compilation fails only when class name ambiguity comes. And java11+ allows execution without compilations. In that case it execute by first matching class. But I didn't found real refference for this till now., "elimination of limitation at compile time." So in my assumption, SoloLearn just search for main class then it executes, maintaining all main classes as per alphabetical order.. But for other IDEs, we have name it with main top class.java. it raise error definately.. So it's bug for class word. And it's allowed top level public but not recommended for security and safety reasons. That's what I understood but need to find more exact reasons.. Take a look at this.. https://stackoverflow.com/questions/2336692/java-multiple-class-declarations-in-one-file/32102123

+2

Yes. This behaviour limited to Sololearn. In outside actually we need to save file with top level class name and that class only executed at start.. Remain other will depend on its call from main.. I have similar, posted few months ago... This... https://www.sololearn.com/discuss/2099260/?ref=app

+2

George Albert Do not post unnecessarily in others thread. Do not spam and find better ways to deal with your boredom and frustrations. Thanks