• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

private main()

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}

}
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}

}


 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You won't be able to run the program unless you make
the main() method public static void
 
fengqiao cao
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fengqaio,
I got this response:
Main method not public.
 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Uma Viswanathan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Valentin for the confirmation.
Uma
 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See
http://www.javaranch.com/certfaq.jsp#q16
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi valentin,
i guess must it be something to do with machine dependent jvm.. can it be???
regards
maulin.
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ah.... didn't know this!
good to know it.
 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
reply
    Bookmark Topic Watch Topic
  • New Topic