• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why is main static

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 180
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1585
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic