Why this program giving me output instead of complie time error? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

26th Jul 2020, 7:29 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
22 Answers
+ 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.
26th Jul 2020, 7:54 PM
A͢J
A͢J - avatar
+ 6
Preity this thread not covering my answer, thanks for your answer :)
26th Jul 2020, 8:02 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 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...
26th Jul 2020, 7:53 PM
Julia
+ 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
26th Jul 2020, 7:59 PM
Preity
Preity - avatar
+ 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.
26th Jul 2020, 8:25 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 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.
27th Jul 2020, 6:09 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 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).
27th Jul 2020, 10:05 AM
Denise Roßberg
Denise Roßberg - avatar
+ 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.
27th Jul 2020, 4:48 PM
Denise Roßberg
Denise Roßberg - avatar
+ 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. 😊
27th Jul 2020, 7:27 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 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.
27th Jul 2020, 4:22 PM
Denise Roßberg
Denise Roßberg - avatar
+ 3
~ swim ~ Weird. I always thought just the word "class" causes the problem but did not know that it works for some print statements.
27th Jul 2020, 4:26 PM
Denise Roßberg
Denise Roßberg - avatar
+ 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
27th Jul 2020, 4:41 PM
Jayakrishna 🇮🇳
+ 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.......
27th Jul 2020, 4:56 PM
Jayakrishna 🇮🇳
+ 3
~ swim ~ then @Pawan questioner comes to ask you, "why his above Program is fine"... 😂?
27th Jul 2020, 5:03 PM
Jayakrishna 🇮🇳
+ 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
27th Jul 2020, 7:20 PM
Jayakrishna 🇮🇳
+ 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
26th Jul 2020, 8:01 PM
Jayakrishna 🇮🇳
+ 1
P∆WAN M∆URY∆ A top level class can be public or default. My program access specifier is default and my code has all default..both should give compile errors in other IDEs. But am not getting here.. So I thought behaviour is limited to here.. Am in confused more now.. I shloud also frame question as your. But I thought it was allowed because I heard java11 added feature of removed the limitation of 1 top level classe for file.. But am not sure. I am getting exact link.. Java11 update also saying there is no need to compile single file. It can be run directly.. So this may related to Sololearn execution but am not sure.. Interestingly am getting error. But not able to find diference between Program in question and this.. Can any one tell me..? https://code.sololearn.com/cQ05vRLClI4z/?ref=app
27th Jul 2020, 3:43 PM
Jayakrishna 🇮🇳
+ 1
I really want to learn
28th Jul 2020, 1:50 PM
Old money Collector
0
a='1*-2' b = eval(a*2) print(b) The output is 42. How come the output will be 42? Can anyone please explain me #Python
28th Jul 2020, 6:42 PM
Ankush G