• 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 main is declared as static?

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody, I am Krishna, suddenly I got a doubt, why main function is declared as static. I know that java calls the main function using the class name. If a method has to be called using class name it must be declared as static. So we declare main as static while coding.

The java developers can initialize my java class and call the main method using an object. Why they designed the architecture in this way. Is there any specific reason. One of my friend said that if we declare a function as static it will have only one copy n the memory and something. I did not understand that.

Can anyone tell me why main is declared as static?

Thank you all in advance. Have a nice day.
 
Marshal
Posts: 79392
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you can invoke the method before creating an instance of the class.
 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell, I already know that, but some one said there is something else.

Why can't the java developers code in this way, so that, the program can create an object and call the main method.

Is there any other meaning?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is "the program" if the JVM is started through the "java" command (which is not implemented in Java)? How would it get hold of an appropriate object?
 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:What is "the program" if the JVM is started through the "java" command (which is not implemented in Java)? How would it get hold of an appropriate object?



"The program" means some underlying java program, I mean the java.exe program.
 
Ranch Hand
Posts: 174
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The language is specified the way it is. So I see no problem accepting to provide a public static main method.

Every class method (aka static method) can be invoked without a reference to a particular object (you don't need to instantiate an object of the class). Imagine a class MyTestFrame that extends JFrame and declares a bunch of fields ... and a main method. We don't need all that information for simply calling the entry point of the application, we just need the class itself.

Take a look at the reflection api ... there's a difference calling a class method or an instance method:
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Peter Taucher wrote:The language is specified the way it is. So I see no problem accepting to provide a public static main method.



Agreed. Short answer of why is... that is how it is specified.

For a longer answer, you will need to ask the Java designers themselves.

Henry
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"The program" means some underlying java program, I mean the java.exe program.


As I said, java.exe is not implemented in Java.
 
Marshal
Posts: 28257
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

krishna Karthikk wrote:Why can't the java developers code in this way, so that, the program can create an object and call the main method?


You mean that java.exe should create an object of the X class and then call that object's main(String[]) method?

Then there would have to be some rules which allowed java.exe to create an object of the X class. You'd have to make a rule that every class to be executed via main(String[]) would also have to have a zero-argument constructor, so that java.exe would be able to create an object. Or something like that.

That seems like an unnecessary complication to me. Sure, they could have done that. But why would they?
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Always using main() is pretty handy, and it doesn't require a separate piece of
plumbing with its own rules and problems. The only trick is that the name of the
entry method - main() - is hard wired into the launch program. I suppose it could
as easily have been defined like this: Jim ... ...
 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:
You mean that java.exe should create an object of the X class and then call that object's main(String[]) method?

Then there would have to be some rules which allowed java.exe to create an object of the X class. You'd have to make a rule that every class to be executed via main(String[]) would also have to have a zero-argument constructor, so that java.exe would be able to create an object. Or something like that.

That seems like an unnecessary complication to me. Sure, they could have done that. But why would they?



Hi Paul, I understood the point you said. Thanks for the reply.
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Hoglund wrote: . . . the name of the entry method - main() - is hard wired into the launch program. . . .

Presumably a leftover from C; the String[] bit allows one to use command-line arguments, too.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

krishna Karthikk wrote:Hi everybody, I am Krishna, suddenly I got a doubt, why main function is declared as static. I know that java calls the main function using the class name. If a method has to be called using class name it must be declared as static. So we declare main as static while coding.

The java developers can initialize my java class and call the main method using an object. Why they designed the architecture in this way. Is there any specific reason. One of my friend said that if we declare a function as static it will have only one copy n the memory and something. I did not understand that.

Can anyone tell me why main is declared as static?


just look at the javadoc for java.lang.Thread . on the 3rd paragraph:

"When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:...."
Running this code should emphasize what is happening.

Notice that the "main(String[] args)" is a normal static method. The bootstrap thread is just hardwired to invoke the method signature "static void main(String[] args)" to get things rolling.

"One of my friend said that if we declare a function as static it will have only one copy n the memory"
Your friend is wrong. There is always only one copy of a method (regardless of if its static or not). Don't think of an object "having methods". Rather, think of methods performing operations on objects.
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each object has state as stored in its assigned copy of instance variables. The class
itself also has state that's stored in the class (static) variables. The JVM loads just
one copy of each constructor, method and initialization block. Static code can operate
only on the class variables while non-static code operates only on instance variables.
Stack (local) variables are allocated space for each method invocation.

If method3() is invoked in three threads simultaneously, for example, there will be
three copies of its local variables created on the stack. But there is still just one copy
of method3(). The JVM keeps track of each thread's progress through method3() as
the threads are swapped, and where each local variable set is stored.

Jim ... ...
 
Masa Saito
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Hoglund wrote:Each object has state as stored in its assigned copy of instance variables. The class
itself also has state that's stored in the class (static) variables. The JVM loads just
one copy of each constructor, method and initialization block. Static code can operate
only on the class variables while non-static code operates only on instance variables.
......


That is not quite true.
An instance has access to its class, however a class doesn't have access to its instances. Thus, instances can change the state of their class, but a class cannot change the state of any of its instances. Example:



Using an instance of "Main" I changed the static state of Main. The reason I used the reflection is to emphasize all methods (instance and static) belong to classes. I got the method "register()" from the "Main" class, then "register()" was "invoked on" an instance of Main. anyway, this discussion went far afield from what the OP wanted to know. sorry. i'm new and just want to contribute....
 
Jim Hoglund
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Hoglund wrote:... non-static code operates only on instance variables. ...


Quoting my error here, (I was rushing to leave for dinner) this certainly is not true.
Non-static code has access to ALL its class members. Is it correct to say that labeling
a method static restricts its access to only other static members?. The main purpose
of my post was to address duplication of method code.

Jim ... ...
 
Masa Saito
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Hoglund wrote:

Jim Hoglund wrote:... non-static code operates only on instance variables. ...


Quoting my error here, (I was rushing to leave for dinner) this certainly is not true.
Non-static code has access to ALL its class members. Is it correct to say that labeling
a method static restricts its access to only other static members?. The main purpose
of my post was to address duplication of method code.

Jim ... ...


As best as I know, that is correct.
But fields and nested classes in implemented interfaces are also accessible from static methods.


 
krishna Karthikk
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot all of you. I concerned my friend once again, the point is,

If my class has to be initialized, then the main method should be accessed using an object. If there are two instances of main method the jvm will confuse. If the main method is declared as static then only one single copy of main exists. This is what I understood. Is this right?

Thank you all in advance.
 
Masa Saito
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

krishna Karthikk wrote:
If my class has to be initialized,


A class (as opposed to an instance of a class), can be initialized only once. It happens in the "static initializer". Instances of a class are initialized in a constructor. Here is short code that demonstrates what happens:


krishna Karthikk wrote:then the main method should be accessed using an object.


The object is the "bootstrap thread" as already mentioned and clearly written in the javadoc for java.lang.Thread . Please do some work and read javadoc.

krishna Karthikk wrote:If there are two instances of main method


At this point, its best not to think of methods being objects. But if your friend wants to, just tell him he is 101% wrong, and tell him to look at the reflective api.

krishna Karthikk wrote: the jvm will confuse. If the main method is declared as static then only one single copy of main exists.



At this point, you will need to post Java code that shows exactly what you are talking about.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
As far as i know i am posting this

actually when an object is created for a class , the members of the class gets their memory allocated.and all the members can be accessed only thru methods in java.And all the methods are called in main. So main should be declared static without making it necessary of an object to call it....
 
Campbell Ritchie
Marshal
Posts: 79392
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch krishnakiran cherukuri
reply
    Bookmark Topic Watch Topic
  • New Topic