i'm using jdk1.5 n i used 3 classes in one source file n not declared the class as public in which main method is there.but it still works n gives output.how is it possible.
Kaarthick Ramamoorthy
Greenhorn
Joined: Sep 01, 2006
Posts: 23
posted
0
Hi,
1) A class that has main method, need not be public. It may use default modifier.
2) A sourcefile may have any number of non-public class. If it has a public class, it must be the name of the file.
So in your case, all classes are non public and even non public class may contain main(). So no problem.
Its not compulsory that a public method should present in the java source file.
Note: If public class presents then only one class for one source allowed and the name of the source file must match.
Javac will generate .class files for all classes which are coded in the source file and you can have main in each and every class, there is no problem in that.
It will take the main from the class which , will be mentioned at java <className>
Vaibhav Chauhan
Ranch Hand
Joined: Aug 16, 2006
Posts: 115
posted
0
its not a mandatory rule that a class containing main method should have access modifier as public....the rules are as following:
1. class in a file can have access modifier as either public or default. 2. if there are more than one class in a file one class can be public at max(but public access is not mandatory for any class even now). 3. the file name should match the public class.
i hope you are clear now.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.