Console Application Riddle

spatio

New member
Joined
May 9, 2014
Messages
2
Programming Experience
Beginner
I have read that a static method is one which can be called from from anywhere in the solution without instantiating the class containing the method. So here's my question -

W/ A Console Application - Why does the program entry point of execution, i.e. static main (), have to be static. That method generally lies within the 'Program Class' which, in a common sense way is never instantiated in an object sense. So why the necessity for static in 'static main()'.???

Also, It is possible to have the 'static main()' inside of a class which is not static, so doesn't that go against convention? i.e. static methods have to go inside static classes????

thanks Gents,
Frank
 
A static member is one that belongs to the type rather than an instance of the type. It's not that you can call a static method from anywhere without creating an instance; it's that you call a static method on a type rather than an instance of that type and the type can be accessed anywhere. The motivation for defining a static method is not that you can call it anywhere without creating an instance; the motivation is that the method is logically a member of the type and not an instance.

All Windows apps have a Main method declared as static as their entry point, not just Console applications. I have to admit that I don't know all the details of the mechanism but if that was not the case then Windows would have to create an instance of the type that and that would serve no useful purpose.

If a class is static then every member of that class must be static too but any class can have static members.
 
Back
Top Bottom