1-why are we required to create static to main method.-
2-why are we required to create public to main method.
3-can i use int at the place of void in main method
doubt please clear sir as fast as possible
Resolved
Comments (5)
- Popular
- New
- Old
You must be signed in to leave a comment
Ashish RajAnand
29 September 2018, 15:06
sir i want to use int at the place of void in main method
for eg
public static int main(String[ ] args)
{
return 0;
}
my question
3-can i use int at the place of void in main method
why show error

0
Ankush Rajput
29 September 2018, 16:24
No, you cannot use int in place of void in java. Java doesn't allow it for reasons i've mentioned.
Why you wan't to use int in place of void in main ? It is not possible to do that.To whom you'll return the value ?
I think you are confusing java with c and c++. C and c++ allows return value in main but not java.
0
Ankush Rajput
29 September 2018, 14:09
1. Because Static method can be executed without creating any objects. Main is the starting point of a program, if a program has not yet started how can it create objects ? Hence main is static to access it without creating any instance of it.
2. As the main is the first method to be executed in java, it must be available to whoever is starting your program. If you mark it as private then how you are going to access it ? WIthout accessing it, your program won't start.
3. Lets take an example - there are two functions func1 and func2. func1 calls func2 and func2 returns a value to func1.
As i've said, main is always the first function to be executed in a program, if you specify return type, to what function will main return value to ? There is no other function before main.
0
Ashish RajAnand
29 September 2018, 15:47
3. Lets take an example - there are two functions func1 and func2. func1 calls func2 and func2 returns a value to func1.
As i've said, main is always the first function to be executed in a program, if you specify return type, to what function will main return value to ? There is no other function before main.
please explain its brief
0
Ankush Rajput
29 September 2018, 17:08
Consider there are 5 people standing in a line.
5th person has a ball. It passes the ball to 4th person, 4th person passes to 3rd person, 3rd person passes it to 2nd person and 2nd person passes it to 1st person.
Now there is nobody behind 1st person. Who will it return the ball to.? He himself is the number one.
Same is the case with main function. It is the first function to be called in a program. No function can be executed before main. It is the starting point. And since there is no function before main, it cannot return to any function. Hence it is not allowed in java to use return type in main function.
0