Professional Documents
Culture Documents
File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools
Java FAQ
Recent Topics
Register / Login
Author
priyanaka jaiswal Ranch Hand Joined: Jun 03, 2011 Posts: 79
Hi,
why main is static in java ?as i know, main should be called by JVM Before any object creation that why it is declared as static, apart from this any other reasons?
Thanks in advance
fred rosenberger lowercase baba Bartender Joined: Oct 02, 2003 Posts: 10129
Is another reason needed? And in any case...the designers of the language probably COULD have done something else, but nobody here really has any way of knowing why they chose to make the decisions they did.
8
There are only two hard things in computer science: cache invalidation, naming things, and www.coderanch.com/t/611457/java/java/main-declared-static
1/9
7/14/13
I like...
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
. . . the designers of the language probably C OULD have done something else, . . .
In classic Eiffel, Meyer took a completely different approach. You have to call a 4 creation expression as the first instruction, and that corresponds to a constructor. That constructor creates the app. As Fred says, that is how Gosling et al decided to implement Java.
Mansukhdeep Thind Ranch Hand Joined: Jul 27, 2010 Posts: 1156
I like...
. . . the designers of the language probably C OULD have done something else, . . . In classic Eiffel, Meyer took a completely different approach. You have to call a creation expression as the first instruction, and that corresponds to a constructor. That constructor creates the app. As Fred says, that is how Gosling et al decided to implement Java.
What is classic Eiffel and who is Meyer? Also, what is a creation expression?
~ Mansukh Campbell Ritchie Sheriff Joined: Oct 13, 2005 Posts: 32917
Eiffel is a language, designed by Bertrand Meyer, and a creation expression is similar to calling a constructor. I have forgotten the exact syntax.
Ivan Jozsef Balazs Ranch Hand Joined: May 22, 2012 Posts: 513
Things should be as complicated as needed but not more. Is it conceivable that a starting method (the famous main) can be run statically, that is, not needing an instance? Yes, it is very well conceivable all the time around the globe.
1
www.coderanch.com/t/611457/java/java/main-declared-static
2/9
7/14/13
And what if an instance is needed? Then create it in your (static) main and invoke the appropriate method against it. If main were not static, it would be compulsory to create an instance of the class. Not that an empty constructor needs so much resources, but why make it compulsory if there is no reason? Why make the things more complicate than necessary? So it is a non-question to me.
Any class can have a main method. For example, both your Class(1) and Class(2) classes can have a main method, but only one will be called once when your program is ran. When you run the program, for example, java class1 - you tell Java that you wish to START the program using the main method of Class(1). This can be confusing, as you'd think Java would execute each and every main method it finds, but that's not the case. Once Java has found and ran the main method in the class you specified, it will ignore all future main() methods it may find as it's already executed a main method for your program. You can think of main() as the door that leads into your program, once in, the computer won't try to come in again, it's already in the program! I hope this helps you a bit.
James Peterson Whizlabs Java Support Ranch Hand Joined: Feb 26, 2013 Posts: 129
Hi priyanaka , The methods which are declared as static can be accessed using the class name i think this is clear. ok let me explain why you need to give static for main method. As the main() method is starting point of program execution you cannot create object for the class in which main() is declared. when you run a program using java exe then main mehtod is called using class name. Look following way you will compile and run any java programme. javac Programname.java java Programname --------> here JVM call main as Programname.main()
Regards, James
Whizlabs-Software, OC AJP 7 , SC JA 6 , SC JP 6 , OC PJP 7 , OC PJP 7- Upgrade ,SC WC D 5 ,OC EJWC D 6 ,SC BC D 5 ,SC EA 5 , PMP , PMI-AC P Unconditional 100% Test PASS Guarantee!
PrashanthKumar
www.coderanch.com/t/611457/java/java/main-declared-static
3/9
7/14/13
hi James Peterson Apart From That Is There Any Reason??? please Could You Tell?
If main were not static, it would be compulsory to create an instance of the class. Not that an empty constructor needs so much resources,
6
I like...
but why make it compulsory if there is no reason? Why make the things more complicate than necessary? So it is a non-question to me.
Just to play devil's advocate for a bit, it would have been trivial to define the rules such that the main() method had to be non -static, and that the main class had to have a no-arg constructor, or possibly one that took an array of Strings, taken from the command line. It would have been no change at all to the language spec, and a minor change to the JVM spec. Then we'd be writing programs like this:
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 . 0 9 .
p u b l i cc l a s sF o o{ p u b l i cF o o ( ){ / /p r o b a b l yu s u a l l yd o e sn o t h i n g } p u b l i cv o i dm a i n ( S t r i n g [ ]a r g s ){ / /j u s tl i k eo u rc u r r e n ts t a t i cm a i n } }
or maybe this:
view plain c opy to c lipboard print ?
N ote: T ext c ontent in the c ode bloc ks is automatic ally word- wrapped
0 1 . 0 2 . 0 3 . 0 4 . 0 5 . 0 6 . 0 7 . 0 8 .
p u b l i cc l a s sF o o{ p u b l i cF o o ( S t r i n g [ ]a r g s ){ / /m a y b ed on o t h i n g ,m a y b es e ts o m em e m b e rv a r i a b l e sf r o ma r g s } p u b l i cv o i dm a i n ( ){ / /N oa r g sb e c a u s et h ec o n s t r u c t o rt o o kt h e m . / /I fw ew a n tt h e m ,c ' t o rp u t st h e mi nm e m b e rv a r i a b l e s .
4/9
www.coderanch.com/t/611457/java/java/main-declared-static
7/14/13
0 8 . 0 9 . 1 0 . } }
/ /I fw ew a n tt h e m ,c ' t o rp u t st h e mi nm e m b e rv a r i a b l e s .
Or maybe it would be Runnable, and instead of providing main() we'd provide run(). Those of us who've been using Java for a while would be used to it and thinks it's normal and simple. Beginners would be asking, "Why couldn't they just make main() static?" We'd tell them that it's not really any more complicated this way, and it's more OO this way, so it's more consistent with the way we write the rest of our code, so that keeps things simpler overall. My point being, it was probably a fairly arbitrary choice, and while we can backfill advantages to one approach or the other, it's really not that much different either way.
you'd think Java would execute each and every main method it finds
6
I like...
You can think of main() as the door that leads into your program, once in, the computer won't try to come in again, it's already in the program! I hope this helps you a bit.
Or we can think of it as just another step that's called as a result of other steps, just like the way you described main() calling other methods. Because that's exactly what it is. We type j a v aM y C l a s s , which causes the shell to launch the java executable and pass it "MyClass" as an arg. That executable is just a program that follows its own instructions (starting with its own main in most cases), and those instructions eventually lead it to load a class called MyClass and execute the main() method there, which in turn has instructions to call other code. And none of this has anything at all to do with main() being static.
ok let me explain why you need to give static for main method. As the main() method is starting point of program execution you cannot create object for the class in which main() is declared.
www.coderanch.com/t/611457/java/java/main-declared-static
5/9
7/14/13
6
I like...
It would certainly have been possible for the JVM spec to be defined that way. It just so happens that it wasn't. So the reason that main() is static is because the designers choose that it is so. They could just as easily have chosen to make it non-static, as I described in my earlier post.
when you run a program using java exe then main mehtod is called using class name. Look following way you will compile and run any java programme. javac Programname.java java Programname --------> here JVM call main as Programname.main()
hi James Peterson Apart From That Is There Any Reason??? please C ould You Tell?
6
I like...
It was a design decision. It could have pretty easily been either "main() will be static" or "main() will be non-static." Either one would work, and there wouldn't be much difference. They had to choose something though, and it turns out they chose static, probably for the reason of simplicity, as Ivan mentioned earlier.
Or we can think of it as just another step that's called as a result of other steps...
Or indeed as: "that's the way C did it. Java is a "better" version of C, and people are 7
I like...
familiar with the paradigm". @PrashanthKumar: "Why" questions like this aren't generally very productive. Quite apart from the fact that, on a forum like this, you'll only be getting our opinion , it suggests that you're just "dissatisfied with how Java does it"; which is fine if you have a viable alternative.
The fact is: it is what it is; and the decisions behind it are probably too ancient to
www.coderanch.com/t/611457/java/java/main-declared-static 6/9
7/14/13
work out. If you can't live with it, suggest an alternative to the Java Community Process; otherwise, there are plenty of other languages out there (many of which, incidentally, use the JVM). Winston
Isn't it funny how there's always time and money enough to do it WRONG? PrashanthKumar Vuthuru Greenhorn Joined: May 14, 2013 Posts: 8 Ivan Jozsef Balazs Ranch Hand Joined: May 22, 2012 Posts: 513
Putting the questions like this nothing will happen we will learn new things they may think differently regarding the question who posted.It Is Benfit for all not only for me???
Well in C there is not much choice if we ponder whether main should belong to a class instance or not. In C there are no classes. Referring to C++ would have even be better. BTW in C functions or variables can be labelled as "static" but that means they is not visible outside their compilation unit. So you can label "main" as static but then chances are that linking chokes like this: Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland Error: Unresolved external '_main' referenced from C:\LANGS\BORLAND\BCC55\LIB\C0X32.OBJ
Ivan Jozsef Balazs Ranch Hand Joined: May 22, 2012 Posts: 513
It is a convention how Java handles launching a program. It is a kind of framework how to start a Java program and how to pass the command line arguments, tailored after C (and C++), where this was introduced with a reason and proved to be viable. There are other frameworks like servlets where another approach is taken with a reason. There you have to provide a no-arg constructor, so that the container can instantiate the class and perform init before putting it into use.
Jeff Verdegan
7/9
7/14/13
Putting the questions like this nothing will happen we will learn new things they may think differently regarding the question who posted.It Is Benfit for all not only for me???
6 What?
I like...
6
I like...
Yeah, that was kind of my point too. Only I was a lot windier in expressing it.
There are other frameworks like servlets where another approach is taken with a reason. There you have to provide a no-arg constructor, so that the container can instantiate the class and perform init before putting it into use.
Yeah, but they still run in a container that gets started by the JVM with a p u b l i c s t a t i cv o i dm a i n ( S t r i n g [ ]a r g s )method.
Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp
Similar Threads
www.coderanch.com/t/611457/java/java/main-declared-static
Abstract classs
8/9
7/14/13
Abstract classs Regarding Serialization Class not found? can't be resolved to a type error Doubt about ClassCastException (was: Doubt)
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 08:26:15 .
www.coderanch.com/t/611457/java/java/main-declared-static
9/9