aspose file tools*
The moose likes Beginning Java and the fly likes Why main is declared as static? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why main is declared as static?" Watch "Why main is declared as static?" New topic
Author

Why main is declared as static?

krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
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.


There is no rule that we all should know everything. Lets learn few things of everything here.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
So you can invoke the method before creating an instance of the class.
krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
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?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
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?


Ping & DNS - my free Android networking tools app
krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
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.
Peter Taucher
Ranch Hand

Joined: Nov 18, 2006
Posts: 174
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:


Censorship is the younger of two shameful sisters, the older one bears the name inquisition.
-- Johann Nepomuk Nestroy
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
"The program" means some underlying java program, I mean the java.exe program.

As I said, java.exe is not implemented in Java.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

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?
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
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 ... ...


BEE MBA PMP SCJP-6
krishna Karthikk
Ranch Hand

Joined: Mar 16, 2010
Posts: 92
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
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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.
Masa Saito
Greenhorn

Joined: Jun 08, 2010
Posts: 14
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

Joined: Jan 09, 2008
Posts: 525
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

Joined: Jun 08, 2010
Posts: 14
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

Joined: Jan 09, 2008
Posts: 525
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

Joined: Jun 08, 2010
Posts: 14
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

Joined: Mar 16, 2010
Posts: 92
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

Joined: Jun 08, 2010
Posts: 14
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.
krishnakiran cherukuri
Greenhorn

Joined: Jun 11, 2010
Posts: 1
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....


krishnakiranchowdary
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Welcome to JavaRanch krishnakiran cherukuri
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why main is declared as static?