Hi, I read this in some notes. But am not able to figure it out. If a .java file does not contain a public class or interface it can have any name. If a class is not public, it says that you can have any name i.e(.java) for that file. But I tried the following code class b{ public static void main(String a[]){ } } it compiled with out any error. when I saved the file as b1.java. but it gave runtime exception. Can any one explain me what is it actually. Or my understanding is wrong Thanks in advance
Arathi<br />Sun Certified Java Programmer
Jim Hall
Ranch Hand
Joined: Nov 29, 2001
Posts: 162
posted
0
How did you try to run it. The compiler will create a "b.class" file. So in order to execute the class file from the command line type, "java b", not "java b1". [This message has been edited by Jim Hall (edited December 13, 2001).]
Bill Krieger
Ranch Hand
Joined: Sep 27, 2001
Posts: 53
posted
0
You can have a class in a file where the file name is not the same as the class name, as you demonstrated. You cannot run its "main" method, however. The main method you want to invoke from the command line must be in a public class whose name matches the file name.
Jim Hall
Ranch Hand
Joined: Nov 29, 2001
Posts: 162
posted
0
In a file called XYZ.java is the following code: <code><pre> class A { public static void main(String a[]) { System.out.println("A"); } } class B { public static void main(String a[]) { System.out.println("B"); } }
class C { public static void main(String a[]) { System.out.println("C"); } } </pre></code> Each has a main method. After compiling XYZ.java, the compiler creates "A.class", "B.class", and "C.class". I can run all three of these classes from the command line without having them declared public.
[This message has been edited by Jim Hall (edited December 13, 2001).]