Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class name and file name should be same??

 
Eureka Jana
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic