You are on page 1of 9

7/14/13

main() is declared as static (Beginning Java forum at JavaRanch)

File APIs for Java Developers Manipulate DOC, XLS, PPT, PDF and many others from your application. http://aspose.com/file-tools

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Java Beginning Java

Author
priyanaka jaiswal Ranch Hand Joined: Jun 03, 2011 Posts: 79

main() is declared as static


posted 5/13/2013 2:12:53 PM

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

posted 5/13/2013 5:28:52 PM

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...

main() is declared as static (Beginning Java forum at JavaRanch)

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Campbell Ritchie Sheriff Joined: Oct 13, 2005 Posts: 32917

posted 5/13/2013 5:59:34 PM


fred rosenberger wrote:

. . . 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...

posted 5/13/2013 6:01:39 PM


Campbell Ritchie wrote: fred rosenberger wrote:

. . . 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

posted 5/13/2013 6:07:35 PM

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

posted 5/14/2013 3:45:21 PM

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

And what if an instance is needed?

2/9

7/14/13

main() is declared as static (Beginning Java forum at JavaRanch)

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.

PrashanthKumar Vuthuru Greenhorn Joined: May 14, 2013 Posts: 8

posted 5/14/2013 3:50:12 PM

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

posted 5/14/2013 3:57:57 PM

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

posted 5/14/2013 4:06:51 PM

3/9

7/14/13

PrashanthKumar Vuthuru Greenhorn Joined: May 14, 2013 Posts: 8

main() is declared as static (Beginning Java forum at JavaRanch)

posted 5/14/2013 4:06:51 PM

hi James Peterson Apart From That Is There Any Reason??? please Could You Tell?

Jeff Verdegan Bartender Joined: Jan 03, 2004 Posts: 6109

posted 5/14/2013 4:11:33 PM


Ivan Jozsef Balazs wrote:

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 . } }

main() is declared as static (Beginning Java forum at JavaRanch)

/ /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.

Jeff Verdegan Bartender Joined: Jan 03, 2004 Posts: 6109

posted 5/14/2013 4:15:46 PM


PrashanthKumar Vuthuru wrote:

you'd think Java would execute each and every main method it finds

6
I like...

Why would one think that?

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.

Jeff Verdegan Bartender Joined: Jan 03, 2004 Posts: 6109

posted 5/14/2013 4:18:34 PM

James Peterson wrote:

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

main() is declared as static (Beginning Java forum at JavaRanch)

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()

None of which implies that it has to be static.

Jeff Verdegan Bartender Joined: Jan 03, 2004 Posts: 6109

posted 5/14/2013 4:20:07 PM


PrashanthKumar Vuthuru wrote:

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.

Winston Gutkowski Bartender Joined: Mar 17, 2011 Posts: 4972

posted 5/14/2013 8:45:49 PM


Jeff Verdegan wrote:

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

main() is declared as static (Beginning Java forum at JavaRanch)

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

posted 5/15/2013 9:40:01 AM

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???

posted 5/15/2013 9:46:11 AM

That's the way C did it.

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

posted 5/15/2013 10:16:23 AM

Just to play devil's advocate for a bit,

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.

posted 5/15/2013 10:59:01 AM www.coderanch.com/t/611457/java/java/main-declared-static

Jeff Verdegan

7/9

7/14/13

main() is declared as static (Beginning Java forum at JavaRanch)

Jeff Verdegan Bartender Joined: Jan 03, 2004 Posts: 6109

posted 5/15/2013 10:59:01 AM


PrashanthKumar Vuthuru wrote:

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...

I have no idea what you're trying to say here.

Jeff Verdegan Bartender Joined: Jan 03, 2004 Posts: 6109

posted 5/15/2013 11:01:18 AM


Ivan Jozsef Balazs wrote:

Just to play devil's advocate for a bit,

6
I like...

It is a convention how Java handles launching a program.

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

subject: main() is declared as static

Similar Threads
www.coderanch.com/t/611457/java/java/main-declared-static

Abstract classs

8/9

7/14/13

main() is declared as static (Beginning Java forum at JavaRanch)

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 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/611457/java/java/main-declared-static

9/9

You might also like