Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Source File

 
Rana Thakur
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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");
}
}
 
Bala Arul
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • 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.

 
Siva Prasad
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • 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
 
Seema Das
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic