Why main(String args[]) method needs to be public?
Considering a simple Helloworld program
class Helloworld{
public static void main(String args[]){
System.out.println("This is Hello World Program");
}
}
The above program compiles and runs smoothly.:rolleyes:
The question is I have not declared class public.Yet JVM can access my code and run it down.But if I remove
pubic modifier from main method signature..........i.e
class Helloworld{
static void main(String args[]){
System.out.println("This is Hello World Program");
}
}
it wont compile.I mean if it can access my class why cannot the main method?
Re: Why main(String args[]) method needs to be public?
The simple answer is because that's what the language designers decided. It's probably best to not worry about it too much. I could come up with a few logical reasons, but the simple fact is, that's just how it is.
Re: Why main(String args[]) method needs to be public?
Quote:
Originally Posted by
KevinWorkman
The simple answer is because that's what the language designers decided. It's probably best to not worry about it too much. I could come up with a few logical reasons, but the simple fact is, that's just how it is.
Hey please let me know the reason.....it would be great.......! else this question will start eating my until I am convienced....
Thanks a lot
Re: Why main(String args[]) method needs to be public?
Quote:
Originally Posted by
rohan22
Hey please let me know the reason.....it would be great.......! else this question will start eating my until I am convienced....
Thanks a lot
Like I said, it doesn't really matter why. There probably isn't a "this is why main must be public" section of the JLS. Actually, in some of the reading I'm doing, they claim that main doesn't have to be public at all- although my testing does not support that claim.
Can *you* think of any reasons why it makes more sense for main to be public? Note- Just because a JVM *can* do something, does that mean it *should* do something?
Re: Why main(String args[]) method needs to be public?
Quote:
Originally Posted by
KevinWorkman
Like I said, it doesn't really matter why. There probably isn't a "this is why main must be public" section of the JLS. Actually, in some of the reading I'm doing, they claim that main doesn't have to be public at all- although my testing does not support that claim.
Can *you* think of any reasons why it makes more sense for main to be public? Note- Just because a JVM *can* do something, does that mean it *should* do something?
If main doesn't work without public then I assume there must be valid reason for it to be public.Something related to its syntax specification.
Anyways thanks a lot. Like you my search for this is still ON....!.Update me if u find something logical....
Re: Why main(String args[]) method needs to be public?
Quote:
Originally Posted by
rohan22
If main doesn't work without public then I assume there must be valid reason for it to be public.Something related to its syntax specification.
Anyways thanks a lot. Like you my search for this is still ON....!.Update me if u find something logical....
As far as i know, main() is called by JVM and it must has the ability to get seen of outside of the class so it's declared as public.
Coz, in Java if you don't declare with any access specifier, it's package protected, and your application can contain multiple packages, so it must have to be public to get known and seen by JVM.
Re: Why main(String args[]) method needs to be public?
Quote:
Originally Posted by
Mr.777
As far as i know, main() is called by JVM and it must has the ability to get seen of outside of the class so it's declared as public.
Coz, in Java if you don't declare with any access specifier, it's package protected, and your application can contain multiple packages, so it must have to be public to get known and seen by JVM.
Yea i got your point.....As in java if we don't specify with any access specifier its package protected.I totally agree with you.
Coming back to my piece of code:-
class Helloworld{
public static void main(String args[]){
System.out.println("This is Hello World Program");
}
}
When JVM can access my class which is package protected(As shown i have not preceded with any access specifier to my class signature),why cannot JVM access my main method without public access specifier.
Either this code is logically reasonable:- (Will compile and run)
public class Helloworld{
public static void main(String args[]){
System.out.println("This is Hello World Program");
}
}
OR this one: (Will compile but wont run....reason....."Main method not public")
class Helloworld{
static void main(String args[]){
System.out.println("This is Hello World Program");
}
}
But this one(the original one) is bothering me:-
Bottom line "When JVM can access default/package protected class......why not main(String args[]) method"
Heys Guys one more thing to add.I have google'd and got to know that in java 1.4 or 1.3 release the main(String args[]) method could have
been declared even private and protected but was later changed in java's subsequent versions......Its just an information exchange.
Lets not change the bottom line of my question.
Thank You!
Re: Why main(String args[]) method needs to be public?
Quote:
Bottom line "When JVM can access default/package protected class......why not main(String args[]) method"
JVM never knows if you have multiple packages in your application or not. So i guess, logically it actually gives run time error only to avoid later run time errors (if there will be multiple packages).
Re: Why main(String args[]) method needs to be public?
Quote:
Originally Posted by
rohan22
I have google'd and got to know that in java 1.4 or 1.3 release the main(String args[]) method could have
been declared even private and protected but was later changed in java's subsequent versions.
That makes sense. Guess the examples I saw of making main private were older, which is why they don't work on my JVM.
Quote:
Originally Posted by
Mr.777
JVM never knows if you have multiple packages in your application or not. So i guess, logically it actually gives run time error only to avoid later run time errors (if there will be multiple packages).
I'm not sure how packages impact the scope of main. Sure, it can have package scope, but that wouldn't lead to runtime errors (in fact, as the OP pointed out, the requirement that main be public is relatively new to the language). His question is, why did Java make the switch?
I did some more research, and this is the brunt of my findings: Cafe au Lait Special Report: Non-public main() method
Apparently, Java originally required main to be public, as it does now. Then there were a few versions that did not enforce that rule, which may or may not have been a bug. For more info, check out the discussions in this bug report: Bug ID: 4252539 private void main(String[] args) not checked by jdk1.2 vm
And although there is no "why" section, the JLS does indeed require that main be public: Execution "The method main must be declared public, static, and void."
I hope that helps. But the answer to your original question of "why" is still mostly "because that's the rules". We could come up with logic saying that letting private methods be available violates OOP principles on which Java is founded, or that forcing main to be public makes it easier to do testing, compounding, and scripting of programs. Or we could come up with logic arguing that main should be allowed to be private. Either way, it doesn't matter. It is the way it is. It's like saying, why is ArrayList called ArrayList and not ListArray? That's just what the language designers decided. There might be logic behind it, and there might be arguments against it, but it doesn't change the fact of the matter.
Re: Why main(String args[]) method needs to be public?
Quote:
Originally Posted by
KevinWorkman
That makes sense. Guess the examples I saw of making main private were older, which is why they don't work on my JVM.
I'm not sure how packages impact the scope of main. Sure, it can have package scope, but that wouldn't lead to runtime errors (in fact, as the OP pointed out, the requirement that main be public is relatively new to the language). His question is, why did Java make the switch?
I did some more research, and this is the brunt of my findings:
Cafe au Lait Special Report: Non-public main() method
Apparently, Java originally required main to be public, as it does now. Then there were a few versions that did not enforce that rule, which may or may not have been a bug. For more info, check out the discussions in this bug report:
Bug ID: 4252539 private void main(String[] args) not checked by jdk1.2 vm
And although there is no "why" section, the JLS does indeed require that main be public:
Execution "The method main must be declared public, static, and void."
I hope that helps. But the answer to your original question of "why" is still mostly "because that's the rules". We could come up with logic saying that letting private methods be available violates OOP principles on which Java is founded, or that forcing main to be public makes it easier to do testing, compounding, and scripting of programs. Or we could come up with logic arguing that main should be allowed to be private. Either way, it doesn't matter. It is the way it is. It's like saying, why is ArrayList called ArrayList and not ListArray? That's just what the language designers decided. There might be logic behind it, and there might be arguments against it, but it doesn't change the fact of the matter.
Awesome..... thanks a lot.....But we are back to the square in giving the answer....."the JLS says so....".Yes i agree not much information
is available on net on this topic.But will keep the thread alive.If anyone of us gets to something interesting information about it,
lets keep updating.
Happy coding !
Cheers.
Re: Why main(String args[]) method needs to be public?
I'm all for that, but I really doubt there's a definitive "this is why things are the way they are" source of information. We can come up with reasons that forcing main to be public makes sense, and we can come up with reasons that not doing so makes sense too. In the end, the language designers had to choose one. The fact that one of the releases did not enforce it would appear to be a bug, since it has been "fixed" in subsequent releases.
Also, there is a chance that this is implementation dependent. Perhaps some JVMs require main to be public and some do not. This would seem to violate the JLS, but different people can interpret the same sentence differently.
In the end, it's just one of those things that's simply true. Imho, your time is better spent elsewhere, such as writing code or reading tutorials, than it is spent picking apart the thought processes of the language designers.
Or you could email James Gosling and see whether he replies, haha.
Re: Why main(String args[]) method needs to be public?
Quote:
I'm not sure how packages impact the scope of main. Sure, it can have package scope, but that wouldn't lead to runtime errors (in fact, as the OP pointed out, the requirement that main be public is relatively new to the language). His question is, why did Java make the switch?
His question was later about java switch, but the actual question is why if not provided any access modifier to main() generates run time error?
And i guess yes, package scope matters alot. If we can say that it is specified in the JVM, so this can be the one of the reasons, (not very sure but still makes sense).