0

Why the result don’t make sense!!?

hi guys The result doesn’t seem right sometimes it give 72 or no output what’s wrong public class Program { static int gar = 6; public static int lmeen(int gleeb){ int mm = gleeb; mm = 6; return mm; } public static void main(String[] args) { } } public class name { public static void main (String[] args) { int gg = Program.lmeen(0); int gml = Program.gar + gg * 10; System.out.println(gg+gml); } }

4th Jul 2020, 6:22 PM
Khaled Almutairi
2 odpowiedzi
+ 3
Generally you can name a Java class anything if it doesn't contain a public class. But if it does, you can have at most 1 public class in a Java file and the name of that file should be the class name. Your code will not even compile on an IDE because you have 2 public classes in the same file. You can have 'n' classes and 'n' main methods in a single Java file. To execute each of them individually, keep all the classes default and not public. Give any name to the class and save it. Now go to command prompt- javac file_name.java // this will compile the java file and create .class files for each class in the file. So you can individually execute them. java class_name // this will execute a particular class with the main method from the java file.
4th Jul 2020, 8:11 PM
Avinesh
Avinesh - avatar
+ 1
Here, You have 2 programs in this 1 file. 2 main functions.. So ones the empty main function, from Program class. get executing, ones the main function from class name is get executing... So 1st class Main result is nothing, and 2 main resulting output 72... This is due to Sololearn compiler but actually Systems, real time we save file with class name which have main method and when compiled only that main get executed automatically.. Other main will not get executed until called explicitly... Hope this helps you...
4th Jul 2020, 6:41 PM
Jayakrishna 🇮🇳