Why main is declared as public in its declaration.
public static void main(String [] args)
{
// code
}
plz. help me out.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
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.
Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!