• Post Reply Bookmark Topic Watch Topic
  • New Topic

main() is declared as static  RSS feed

 
priyanaka jaiswal
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote: . . . the designers of the language probably COULD 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.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
fred rosenberger wrote: . . . the designers of the language probably COULD 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?
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 X Peterson
Whizlabs Java Support
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
PrashanthKumar Vuthuru
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi James Peterson

Apart From That Is There Any Reason??? please Could You Tell?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
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:


or maybe this:


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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PrashanthKumar Vuthuru wrote:you'd think Java would execute each and every main method it finds


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 java MyClass, 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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PrashanthKumar Vuthuru wrote:hi James Peterson

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


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
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 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 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
 
PrashanthKumar Vuthuru
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???


What?

I have no idea what you're trying to say here.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:
Just to play devil's advocate for a bit,


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 public static void main(String[] args) method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!