0

hi
i can't undestanding this why the main function in java is static and static function can call using class name. And why we declare main function as public. i can not understanding this procedure of main function in java. plz help me.

2
Contributors
1
Reply
21
Views
2 Years
Discussion Span
Last Post by stultuske
3

first of all: java doesn't have functions, it has methods. yes, there is a difference.

the main method has to be static, since it is the entry point of the application, the very first thing that runs.
if it wasn't static, you would need an instance of the class to call it on, but how can you have an instance if your application isn't running yet?

we declare it public, so we will be able to call it from "anywhere", since that is what we need to do. we're not yet in the running application, which means that package, protected, and most importantly, private scopes would make the method unaccessible.

here 's a nice read about it.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.