0

Java problem?

when we make public class name same as file name but we cannot make public class name when we write different class name and file name. why?

22nd Sep 2020, 9:19 AM
Gaurav Rawat
Gaurav Rawat - avatar
6 Answers
+ 1
If you are making a class public in a Java file then the name of the file must match the public class name and you can have only 1 public class in a Java file. If at all you want to name your file something else then you shouldn't have even a single public class in your Java file. Program.java public class Program{ public static void main(String[] args){ } } Anything.java class A{ public static void main(String[] args){ } class B{ } Your question isn't clear. This is from what I have understood from the question.
22nd Sep 2020, 10:14 AM
Avinesh
Avinesh - avatar
+ 2
Avinesh thanks a lot 😇😇
22nd Sep 2020, 10:45 AM
Gaurav Rawat
Gaurav Rawat - avatar
+ 1
Avinesh yes you got it This is what i was saying but i want to know why we should not make public class in case of different file name and class name
22nd Sep 2020, 10:19 AM
Gaurav Rawat
Gaurav Rawat - avatar
+ 1
By convention if you make a class public then the file name must match the class name. So if you are going to make a class public and give a different name, then the compiler will throw an error.
22nd Sep 2020, 10:43 AM
Avinesh
Avinesh - avatar
+ 1
Gaurav Rawat you're welcome.
22nd Sep 2020, 10:46 AM
Avinesh
Avinesh - avatar