Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

private main()

 
fengqiao cao
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • 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);
}

}
 
Roopa Bagur
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
  • 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);
}

}

 
Nain Hwu
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Fengqaio,
I got this response:
Main method not public.
 
Uma Viswanathan
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
  • 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
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you very much Valentin for the confirmation.
Uma
 
Jim Hall
Ranch Hand
Posts: 162
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi valentin,
i guess must it be something to do with machine dependent jvm.. can it be???
regards
maulin.
 
Fei Ng
Ranch Hand
Posts: 1244
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah.... didn't know this!
good to know it.
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
  • 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.
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic