• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

class name and file name should be same??

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic