wood burning stoves
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

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "public class in Java program file" Watch "public class in Java program file" New topic

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

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
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

Joined: Aug 20, 2001
Posts: 1824

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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
subject: public class in Java program file
It's not a secret anymore!