File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why is main static

 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me why main() is made static.What could have been the problem if is not static?Or are there any performance issues?
 
saurav sarkar
Ranch Hand
Posts: 180
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
main is static because it is the entry point
for the program and we can not instantiate any
object before calling the main, so it is static.
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That i understand that it is entry point and we cannot instantiate any object before calling the main.

But,if jvm would have called main after making instance of the class,what could have been the problem.
There must be some reason that main is accessed before making object of the class.
In C/C++,main is not static,But in java it is made static,so i thought why?
 
vatsalya rao
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Static" methods are called with out the need of an object.

main is the entry point in java program.AS no object is available at the starting of the program main is made static so that it can be called.
 
Vassili Vladimir
Ranch Hand
Posts: 1585
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

It's static so that the JVM can invoke it ...

Best of luck ...
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
entry point in c/c++ is not static.but this has been introduced in java.so there must be some reason for this.May there be some memory issues or performance issues??i don't know??
 
saurav sarkar
Ranch Hand
Posts: 180
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you mean to say the JVM internally should make an object
then call the main method?...............
for that i have a concern

What will happen to other methods of the class.
Then we dont have to create the object and this
defeats our purpose of making an object.

C++ i dont know, may be some OOPs experts can
put some light on this.
 
Peter Chase
Ranch Hand
Posts: 1970
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid there's quite a lot of misinformation in the above responses. Unfortunately, to understand why, it's necessary to know rather more than a beginner typically wants about how a Java application starts up.

The Java Language Specification specifies that the standard entry point for a Java program is main() and that it is static. However, they could have specified a different method and they could have made it non-static; they just had to make some choice.

The entry point is something that the Java application launcher implements. A launcher program is the native program that creates a JVM, finds the initial class[es] and runs the initial method[s] (the "entry point[s]"). These programs are written using the Java Invocation Interface, which is part of the Java Native Interface.

The typical JDK and JRE come with the "java[.exe]" launcher. It is this launcher that dictates that the program's entry point is main() and that it is static.

I could (and have) write a launcher that runs a different method than main. I could (but haven't) write a launcher that instantiated an object of some class then ran a non-static method.

Regarding C++, the main() entry point in C++ is actually a C function. As such, it is rather outside the object-oriented world. If you declare a C function static, it means something different to what it does in Java or C++ (it's closer to "private", actually). However, any C function is more like a static method than it is like a non-static method.
 
Raj Kumar Bindal
Ranch Hand
Posts: 418
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all ranchers.So, from the responses, i conclude that,<b>There is no such objective of making main static.It is just a matter of choice and that's all.</b>
Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic