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?
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.
+ 2
Avinesh thanks a lot đđ
+ 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
+ 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.
+ 1
Gaurav Rawat you're welcome.