hi, there I found that there is not side effect to define main()with private, protect, or default. is it ture all the time? The follwing code return the same output if i change the "private" to pulbic or portected. public class THIS{ private static void main(String args[]){ int i=1;
System.out.println(i); }
}
Roopa Bagur
Ranch Hand
Joined: Nov 03, 2000
Posts: 267
posted
0
My understanding of this concept is that main() is just another method in a class & therefore can have any access modifier.. a main() method can be final & can be overloaded too..
Originally posted by fengqiao cao: hi, there I found that there is not side effect to define main()with private, protect, or default. is it ture all the time? The follwing code return the same output if i change the "private" to pulbic or portected. public class THIS{ private static void main(String args[]){ int i=1;
System.out.println(i); }
}
Nain Hwu
Ranch Hand
Joined: Sep 16, 2001
Posts: 139
posted
0
You won't be able to run the program unless you make the main() method public static void
fengqiao cao
Ranch Hand
Joined: Oct 26, 2001
Posts: 71
posted
0
hi, Nain could you please try to compile and run the following code? public class THIS{ private static void main(String args[]){ int i=1;
System.out.println(i); }
}
Nain Hwu
Ranch Hand
Joined: Sep 16, 2001
Posts: 139
posted
0
Fengqaio, I got this response: Main method not public.
Uma Viswanathan
Ranch Hand
Joined: Jun 14, 2001
Posts: 126
posted
0
In my m/c, private is working fine...but let us know what JLS and some resources tell us... JLS section 12.1.4 tells us that "The method main must be declared public, static, and void. It must accept a single argument that is an array of strings." JQ+ notes and tips also says that "Main method can also be final, native, synchronized. No matter whether other declarations (like private, protected) work on your m/c, for the purpose of the exam, it should be public." From the forum http://www.javaranch.com/ubb/Forum1/HTML/000124.html, newer jdk's allow main to be non-public. However the language specification and the virtual machine specification do say that main should be public. Valentin: Even though JDKs allow main to be non-public, the language requirement (and also for the exam purpose) is that the main method must be public. Could you please confirm this? Hope this helps... Uma
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
posted
0
yes Uma, I'd say so, but I've tried that code with several compiler, some accept and some don't... My advice: for the purpose of the exam, stick to the JLS HIH ------------------ Valentin Crettaz Sun Certified Programmer for Java 2 Platform
What i understand from the previous discussion is... private static void main(String[]) is allowed by some JVMs. But JLS says that signature should be ... public static void main(String[]) Then in exam, if the correct signature for main is asked then, what should we select? as even private is allowed by some JVM . then the option, private static void main(String[]) is valid or not? Or we shd strictly follow JLS? thanx in advanc. Rashm.
Ragu Sivaraman
Ranch Hand
Joined: Jul 20, 2001
Posts: 464
posted
0
Originally posted by Rashmi Gunjotikar: What i understand from the previous discussion is... private static void main(String[]) is allowed by some JVMs. But JLS says that signature should be ... public static void main(String[]) Then in exam, if the correct signature for main is asked then, what should we select? as even private is allowed by some JVM . then the option, private static void main(String[]) is valid or not? Or we shd strictly follow JLS? thanx in advanc. Rashm.
I burned myself in this issue Different compiler give different results. As VAL said, stick to JLS and as always public static void main(String args[]) will be the appropriate and let it be the FINAL ANSWER Ragu