Originally posted by sunilkumar ssuparasmul:
whichever class has main method should be the name of class .
Originally posted by bhakti soman:
i have compiled this code
public static void main(String arg)
saved it as demo.java works fine with me.
that means you need not save file same as that class which contains main mehod.
please help me if i am wrong.
Originally posted by meena sukumar:
Java requires that the classname be the same as the name of the file.
Although ,your code had a class called Bhakti in a file named demo.java it would not give you any compile time error but at runtime u'll get the folllowing message
"Exception in thread "main" java.lang.NoClassDefFoundError:demo"
Originally posted by atin sehgal:
There are a few things which i think I shd share with u first so that i am able to convince u.
1) The mofdifier.The modifier here is defaulty ansd not public.
2) Classs level whether top level or anyother..
If the modifier is public and class is top level its a necessary condition that for compiling that
a) Source Name shd be same as class name
For efficient running(no run time error_)
a) Main method shd b perfect.
Now the case of yr example..
Your class has no access modifir => it translates to friendly or default access.
Hence the condition for efficient compiling in this case is that
1) source file != name of class file.
they can be different.. and yet compile.
For running the condition ois :
That main method shd be correct..
Feel free to touchbase if any query.
I am attching a program that i have done today itself...
//top level class with default access modifier.Class compiles.It will not compile only when main method is not perfect
class abc /* this class will compile even if the file name is different from source file name*/
//class firstdefault//I saved the file earlier as firstdefault.java/.Then changed the name to a different source name.Still it compiled...
public static void main(String args)
/* So conclusion...if a class file is having default access modifier , we can compile the class successfully
a) Even if source name different...from class name.
a)only if main is absolutely correct.
Plz revert in case of doubt or anything wrong abt my code...
Originally posted by amit sanghai:
I have one question.
If there are no public classes or interfaces in our java file what should be the name of that file??
In Zaworski's site it is mentioned that the file name in that case should be different than the names of the classes and interfaces in our file.