I was asked in an interview why we don't first create an instance of the class containing the main
method like all other classes in java. Why is the main
method declared static
?
Asked
Active
Viewed 736 times
-2

Marko Topolnik
- 195,646
- 29
- 319
- 436

user1526671
- 817
- 4
- 16
- 33
-
5I'm sorry I don't understand your question at all. Could you try rephrasing or maybe sample code that you don't understand/can't get to work/doesn't work like you expect it to? – Mat Nov 18 '12 at 11:32
-
3WHAT?! you can create instance of classes with the main function, if that what you are asking. – elyashiv Nov 18 '12 at 11:32
-
A java class can have overloaded constructor. That is why the `main()` function needs the `static` modifier which has only one argument is called in which `we can call our own constructor`. But This is not the case with `Applet`'s which must have a constructor with `no arguments`. Hope you understand it well. And before asking these type of silly questions, learn `programming properly first.` -1 – Sri Harsha Chilakapati Nov 18 '12 at 11:34
-
See [What does 'public static void' mean in Java?](http://stackoverflow.com/questions/2390063/what-does-public-static-void-mean-in-java) – minopret Nov 18 '12 at 11:51
-
1@SriHarshaChilakapati yes i was trying to learn programming properly I was unable to understand so only posted it. I think you should be somewhat polite. – user1526671 Nov 18 '12 at 11:56
-
@user1526671 At first `SO` is not a tutorial site. See http://stackoverflow.com/faq#questions for details on what type of questions you could ask here. And you should try googling it as you can't ask some simple questions. Also check SO for similar questions. http://stackoverflow.com/questions/146576/why-is-the-java-main-method-static – Sri Harsha Chilakapati Nov 18 '12 at 12:06
-
@SriHarshaChilakapati SO doesn't define the threshold of question "complexity"; it only specifies "do the research first". Some simple questions are not easily googled for, such as the notorios "Why does my String comparison return false?". The present question may or may not be a case of insufficient effort, but it is not guilty of being too simple (that would be a snobby remark in any case). – Marko Topolnik Nov 18 '12 at 12:36
-
Seems a reasonable question. I mean, `Applet` is constructed and then lifecycle methods are called on it. – Tom Hawtin - tackline Nov 18 '12 at 19:44
1 Answers
3
As to why the main
method is static
—that is a perfectly reasonable choice for an entry-point method, the first piece of code that gets executed in a new Java VM. If it wasn't static
, there would have to be more constraints and requirements on the entry-point class because the JVM would first need to instantiate it and then invoke main
. This would serve no constructive purpose and would just be an impediment.

Marko Topolnik
- 195,646
- 29
- 319
- 436