Why main is declared as public in its declaration.
public static void main(String [] args)
{
// code
}
plz. help me out.
Printable View
Why main is declared as public in its declaration.
public static void main(String [] args)
{
// code
}
plz. help me out.
Have you tried declaring it private to see what happens?
I'm still fairly new to Java, but remember: main() is inside a class. The class that holds main() is public. And remember that if a method is private, it can only be called from within the class itself.
Try changing your declaration to this:
private static void main(String[] args){
// code
}
and see what happens...
Actually, some JREs allow main to be private. It's a weird quirk, so don't rely on it.
But what do you mean "why is main declared public"? It just is. That's the rule (except for the weird quirk part). It makes sense that it's public, so other things (including the JRE) can have access to it. Otherwise your program couldn't run.
But really, just accept it and move on. It's just a waste of time to think too hard about "why is this named that" in a particular language- it just is.