aspose file tools*
The moose likes Beginning Java and the fly likes public class in Java program file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public class in Java program file" Watch "public class in Java program file" New topic
Author

public class in Java program file

Vrushali Gore
Ranch Hand

Joined: Oct 17, 2005
Posts: 40
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

Joined: Jul 04, 2006
Posts: 18
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

Joined: Oct 17, 2005
Posts: 40
See when u compile a program all the classes in that file are compiled. So there is no issue in that.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
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.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: public class in Java program file
 
Similar Threads
Public Class
Access Modifiers
What's wrong??
URGENT
public class or not