This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Why is main static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why is main static" Watch "Why is main static" New topic

Why is main static

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
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

Joined: Jan 07, 2007
Posts: 180

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.

Be Objectively Oriented.Explore the power of OOPs.
My Blog, Eclipse EMF Query committer.
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
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

Joined: Feb 14, 2007
Posts: 63
"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

Joined: Mar 08, 2007
Posts: 1585

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

Best of luck ...

Vassili ...
SCJP 5.0, SCWCD 1.4, SCJA 1.0
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
entry point in c/c++ is not static.but this has been introduced in there must be some reason for this.May there be some memory issues or performance issues??i don't know??
saurav sarkar
Ranch Hand

Joined: Jan 07, 2007
Posts: 180

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

Joined: Oct 30, 2001
Posts: 1970
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.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
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.
I agree. Here's the link:
subject: Why is main static
Similar Threads
Static keyword???
Static Variable Problem
why static there?