File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes private main() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "private main()" Watch "private main()" New topic
Author

private main()

fengqiao cao
Ranch Hand

Joined: Oct 26, 2001
Posts: 71
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
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
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
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
Fengqaio,
I got this response:
Main method not public.
Uma Viswanathan
Ranch Hand

Joined: Jun 14, 2001
Posts: 126
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
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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Uma Viswanathan
Ranch Hand

Joined: Jun 14, 2001
Posts: 126
Thank you very much Valentin for the confirmation.
Uma
Jim Hall
Ranch Hand

Joined: Nov 29, 2001
Posts: 162
See
http://www.javaranch.com/certfaq.jsp#q16
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi valentin,
i guess must it be something to do with machine dependent jvm.. can it be???
regards
maulin.
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
ah.... didn't know this!
good to know it.
Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
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
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: private main()
 
Similar Threads
problems running code
Nested class in an interface
Accessibility of main()
Private and Protected
Why there are no global variables in Java?