• 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

Source File

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"If there is no public class or interface defined within a source code file, then the file must take on a name that is different that its classes and interfaces."
However the fillowing code compiles and runs fine. Am I missing out something??
//File:: test.java
class test {
public static void main (String args[]){
System.out.println ("hello");
}
}
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If there is no public class or interface defined in a source file, then the source file can get any name.
Arul.

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you have two or more classes in a source file and one of them is public and the other or not, then the file name should the the class name which is declared as public.
If you try to declare more than one public class in a single source file, the compiler will give an error to write each public class in a different file.
Siva Prasad
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rana,
It looks like the file can take any name. It can be one of the class' name too. I tried following with Name.java and B.java and both ways it works fine.
class A {
int i;
}
class B {
int j;
}
class C {
int k;
}
Thanks
 
Rana Thakur
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks. What was bugging me is that couple of places I read that
"If there is no public class or interface defined within a source code file, then the file must take on a name that is different that its classes and interfaces."
But it doesn't look like that. Guess I must have mixed up.
Thanks a lot to all of you.
reply
    Bookmark Topic Watch Topic
  • New Topic