-2

I always thought (assumed) that the Main method was static because you cannot have multiple instances of it (Correct me if that's wrong). The Main method is the start point of your program and thus you can have only one.

So if I have

class Program
{

   static void Main(String[] args)
   { // something
   }

}

class OtherClass
{

   void Test()
   { 
      Program p1 = new Program();
      Program p2 = new Program();
      Program p3 = new Program();
      Program p4 = new Program();

   }

}

all instances of Program will share the same Main method and so there will always be one start point.

Am I correct? Because I just googled this out of curiosity and found DIFFERENT answers to it on the Internet.

Is this explanation ALSO correct for the main method being static?

Thomas Weller
  • 43,638
  • 16
  • 101
  • 185
Ixcioran35
  • 21
  • 6

1 Answers1

1

If the entry point method were not static, someone would need to create an object first. The question is then: create an object of which class?

I always thought (assumed) that the Main method was static because you cannot have multiple instances of it

You can't have instances of methods. Methods reside in the Code section of a DLL and are not copied to the heap. You can only have multiple threads running in the same method.

The Main method is the start point of your program and thus you can have only one.

As before: If we consider signatures, there is only one method, independent if it's static or not, because they are not instantiated.

all instances of Program will share the same Main method ...

Depends on what you understand by the term "share". All objects will have the method, yes.

... and so there will always be one start point.

The reasoning behind it is wrong. You have many instances of Program but that doesn't matter to the amount of methods.

Thomas Weller
  • 43,638
  • 16
  • 101
  • 185
  • If the CLR can find the class containing the main method, it could most definitely instantiate the class. Private constructors seem to be a bigger issue. Not to mention, objects are re-usable, and the CLR would have no further use for the object after calling main, so a functional approach seems more logical – Dioxin Oct 23 '15 at 15:47
  • @VinceEmigh: how would it find the main method? Based on which critiera? – Thomas Weller Oct 23 '15 at 15:50
  • 1
    Based upon the same criteria as it finds the static main method, except for the requriement that it be a static method. However, I see a larger issue in cases where the respective class has no parameterless constructor; without knowing what arguments to pass, the CLR could *not* "most definitely instantiate the class", as suggested by @VinceEmigh . – O. R. Mapper Oct 23 '15 at 15:52
  • 1
    The [same way it currently locates the main method](http://stackoverflow.com/a/6781303/2398375). Of course the CLR will be able to find the main method. It has to call it. @O.R.Mapper That is another good reason, the lack of a proper constructor parameters. The CLR won't know what to pass to it. But those problems are still there with a static method aswell. I'm pretty sure it's due to the CLR not needing to re-use the object, thus making it a waste – Dioxin Oct 23 '15 at 15:53