| Author |
class name and file name should be same??
|
Eureka Jana
Greenhorn
Joined: Sep 04, 2002
Posts: 18
|
|
Hi All, I am having two public classes in a file like this: public class A { // Class body } public class B extends A { // Class body public static void main(String args[]) { // Function body } } When I compile its giving the following error: B.java:1: class A is public, should be declared in a file named A.java Why it is so.Whats wrong in this. Thanks in advance, Jana.
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
Jawahar M, It's nice to see a new face (err - name) hangin' out at JavaRanch. We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy. Thanks Pardner! Hope to see you 'round the Ranch! [ September 11, 2002: Message edited by: Dirk Schreckmann ]
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
The Java compiler only allows one public class definition per file. The file name must match the name of the public class definition contained in the file (if a public class definition exists in the file). So, if you were to do as the error message suggested, your problem would likely be solved. This construct allows the compiler to easily figure out what to do with a source file and the class file definitions. In other languages, such as C, it is my understanding that these rules are not enforced and so, when compiling, the programmer must also provide a make file that tells the compiler what to do. I may have understood that incorrectly as I've never programmed in C.
|
 |
Gregg Bolinger
Ranch Hand
Joined: Jul 11, 2001
Posts: 15230
|
|
Originally posted by Dirk Schreckmann: In other languages, such as C, it is my understanding that these rules are not enforced and so, when compiling, the programmer must also provide a make file that tells the compiler what to do.
Yep, that's pretty much it.
|
 |
 |
|
|
subject: class name and file name should be same??
|
|
|