Click here to Skip to main content
15,906,261 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hi friends

i want to know deeply about "Public static void main(string[]args)".
and can we use main method as private or not?
what is purpose of public,static,void and string[]args in main method?

please kindly anybody give me reply

thanks in advance
Posted
Comments
[no name] 13-Dec-11 9:41am    
did you mean oop or what?

This method can be private and is strongly recommended to be private. The declaring type of this method also can be private. Also, all the project templates generate this method as static private.

Even though the access modifier "private" is usually not used, the generate declaration is private, because this is the default, used when an access modifier is omitted.

Here is why: any assembly including "EXE" could be referenced by other assemblies. Essentially, there is not any fundamental differences between EXE and DLL, class libraries and applications. The only special feature of application is its entry method. This method is designed to be called by .NET loader (or whatever that part of .NET is called, the one starting the .NET application); it is not designed to be called by an assembly referenced the application (the techniques which is used sometimes). You can declare Main as public and call if from some referencing assembly, but only if you knowingly want to violate the common practice.

At the same time, .NET and even any application can call any non-public method throw Reflection. So, for running the application, the public declaration of entry point is not needed.

[EDIT] This question has nothing to do with OOP.

—SA
 
Share this answer
 
v5
Comments
Pramod Harithsa 14-Dec-11 2:07am    
Its made static because the method can be called even without creating the object of the class in which its implemented.
right??
They are static so as to call them even without creating the object of the class they are in.(and also to be called only once)
void since they don't return any value.
string[] args- since strings can take any kind of values in them.(int,decimal,char..)
main indicates the starting point of the program!
 
Share this answer
 
main method is the starting point for any program, we can't declare main method as private, main method won't return anything that's why we are writing
public static void main

[WARNING! Please don't use this answer which is incorrect. Don't get confused. Please see my comments below and correct answer — SA]

 
Share this answer
 
v2
Comments
AZADKUMARREDDY 13-Dec-11 10:22am    
thank u
Sergey Alexandrovich Kryukov 13-Dec-11 10:28am    
For what. This answer is pure lie! The method can be private, and it is always private in all template code, and it is recommended to be private, by all means.
--SA
Sergey Alexandrovich Kryukov 13-Dec-11 10:30am    
You should never post answers unless you are 100% sure. Your lie might costs to much for inexperienced users. I understand this is not intentional, still this is not and excuse. Please be careful.
--SA
Take a look at this link. It contains whole info regarding the Main method.

http://en.wikipedia.org/wiki/Main_function[^]

Hope it helps!

Regards,
Eduard
 
Share this answer
 
Comments
AZADKUMARREDDY 13-Dec-11 10:22am    
thank u friend
Sergey Alexandrovich Kryukov 13-Dec-11 10:51am    
There is nothing in this article answering this question except sample declaration. OP is confused about private Main. If someone did not have this confusion, she/he would see this sample correctly, but with the confusion this is useless. Do you see the point?
I answered in detail, please see.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900