• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

public class in Java program file

 
Vrushali Gore
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In one program file we can have only one public class. Why? Is there any concrete reason behind this.

Can anyone help me in knowing this?
 
Arun kumar Maalik
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is rules in java that when U compile java file compiler search for the name of class that public to make .class file and we know that there is only one class file after compiling the .java file that's why there is only public class in jawa.
Now i think again a dought may be in your mind that ok then what if we want to make another class public so that in another package we can access. It will done by inheritence in the main source file.

" In a forest only one lion isn't it..... "

Arun kumar maalik
akmaalik03@yahoo.com
 
Vrushali Gore
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See when u compile a program all the classes in that file are compiled. So there is no issue in that.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason is so that the Java compiler can find the .java file for the class. If you have A.java and B.java, and B.java references A.java, when you compile B.java A.java will get compiled as well.

If you had class A in with C.java, the compiler wouldn't know to look in C.java to find your A class. Thus, only one public class per file.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic