why can't we write main method without static word ? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
0

why can't we write main method without static word ?

we can create an object of class to call it...

19th Jul 2016, 9:36 AM
jain
jain - avatar
5 Respostas
0
who knows
19th Jul 2016, 9:57 AM
ŠœŠ°Ńˆ Š§ŃƒŃ…Š°Š» ŠŃŠ³ŃŠ½
ŠœŠ°Ńˆ Š§ŃƒŃ…Š°Š» ŠŃŠ³ŃŠ½ - avatar
0
Because static means it is unchanging, if it changed, your code wouldn't be run correctly or at all by the computer then, yes? This is what I suppose, but also while learning the lessons, one of the ones had a yellow tip saying static needs to be in the main method, so, really, why question? I figured it wouldn't cause me any problems, so after asking the question myself, I disregarded it, as it wouldn't affect me. There IS a better answer out there, but sorry to say I don't know that one, and this is what I've got to give you. Hope this helps
19th Jul 2016, 10:35 AM
Tristan
0
The method is static because otherwise there would be ambiguity: which constructor should be called? Especially if your class looks like this: public class JavaClass{ protected JavaClass(int x){} public void main(String[] args){ } } Should the JVM callĀ new JavaClass(int)? What should it pass for x? If not, should the JVM instantiate JavaClassĀ without running any constructor method? I think it shouldn't, because that will special-case your entire class - sometimes you have an instance that hasn't been initialized, and you have to check for it in every method that could be called. There are just too many edge cases and ambiguities for it to make sense for the JVM to have to instantiate a class before the entry point is called. That's whyĀ main is static.
19th Jul 2016, 10:35 AM
Itay Almog
Itay Almog - avatar
0
because we wants to call the main method without creation of any object of class in which the main method is existing. java program always starts from main method this is why we create main method as static.
19th Jul 2016, 12:59 PM
Abhishek dubey
Abhishek dubey - avatar
0
Because by declaring the main class static, it needn't be instantiated. Remember main Is the entry point to your program, therefore the JVM can start execution on main with no need to instantiate it.
21st Jul 2016, 5:49 AM
Eric Gitangu
Eric Gitangu - avatar