wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Why main(String[] args) -method is made static ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why main(String[] args) -method is made static ?" Watch "Why main(String[] args) -method is made static ?" New topic
Author

Why main(String[] args) -method is made static ?

Rubbal Bhusri
Ranch Hand

Joined: Dec 30, 2010
Posts: 71
Why main(String[] args) -method is made static ?
When static methods just mean that, they can be accessed without instantiating a class(means without making objects of the class).
They can just be accessed by their class names e.g. Math.random(), here, Math is the class and random is the static method present in class Math.
What made the founders to make the main method as static ???
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

Rubbal Bhusri wrote:Why main(String[] args) -method is made static ?
When static methods just mean that, they can be accessed without instantiating a class(means without making objects of the class).
They can just be accessed by their class names e.g. Math.random(), here, Math is the class and random is the static method present in class Math.
What made the founders to make the main method as static ???

Well, try to think it from JVM's perspective:

When you order JVM to run a specific class by issuing 'java' command, what it does is - loads that class, and access main method (and import other classes et-cetera).

Now, what if main method is non-static? JVM must be creating an object of the class, and then invoke main method, right? But how would JVM create an object of that class? What if you've declared 5 different constructors for that class? Which one should JVM invoke? And on what basis?
To avoid all this confusion, main method is made static.

Of course, this might not be the complete answer, but this is one scenario I can think of - where it makes much more sense to keep main method as static.

I hope this helps.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Rubbal Bhusri
Ranch Hand

Joined: Dec 30, 2010
Posts: 71
Anayonkar Shivalkar wrote:
Well, try to think it from JVM's perspective:

When you order JVM to run a specific class by issuing 'java' command, what it does is - loads that class, and access main method (and import other classes et-cetera).

Now, what if main method is non-static? JVM must be creating an object of the class, and then invoke main method, right? But how would JVM create an object of that class? What if you've declared 5 different constructors for that class? Which one should JVM invoke? And on what basis?
To avoid all this confusion, main method is made static.

Of course, this might not be the complete answer, but this is one scenario I can think of - where it makes much more sense to keep main method as static.

Yes, of course, it seems the far more better answer, but, since you are also not pretty confident and sure, I mean, as you said "this is one scenario I can think of.", which puts me in more curious condition that what other reasons could possibly be involved behind making main method as static ??
It there are more reasons behind making main method as static, I would pretty like to read them also.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41132
    
  45
I think this one reason alone is sufficient to make it so.


Ping & DNS - my free Android networking tools app
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Actually I must admit to see no reason for main to be an instance method (that is, a non-static one).
It is not that one had to weigh arguments and counter-arguments.
I see no argument on the "instance method" side.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

i suppose you could define the language so that something like this was allowed:

java MyClass(String "hello", int 7), param1 param2 param3

which could create a MyClass object using a specific constructor...but you'd be limited to primitives and possibly Strings, it looks kludgy, and finally, you have no reference to the MyClass object, so technically it would immediately be eligible for garbage collection...

in other words, it would be hard to write the JVM, it gains you nothing, it looks ugly, and you'd have to have all kinds of special exceptions for weird things...to exactly zero benefit.


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

Joined: Dec 30, 2010
Posts: 71
Ivan Jozsef Balazs wrote:Actually I must admit to see no reason for main to be an instance method (that is, a non-static one).
It is not that one had to weigh arguments and counter-arguments.
I see no argument on the "instance method" side.


Please make yourself more clear, I didn't get your point.
Rubbal Bhusri
Ranch Hand

Joined: Dec 30, 2010
Posts: 71
fred rosenberger wrote:i suppose you could define the language so that something like this was allowed:

java MyClass(String "hello", int 7), param1 param2 param3

which could create a MyClass object using a specific constructor...but you'd be limited to primitives and possibly Strings, it looks kludgy, and finally, you have no reference to the MyClass object, so technically it would immediately be eligible for garbage collection...

in other words, it would be hard to write the JVM, it gains you nothing, it looks ugly, and you'd have to have all kinds of special exceptions for weird things...to exactly zero benefit.

What do you mean by define the language ?
and
what are you trying to do here with java MyClass(String "hello", int 7) ?
I don't understand your point !
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

What do you mean by define the language ?


Someone makes up the rules for what is allowed and what is not. For example, someone decided that Strings would be immutable. someone decided that the main method had to be static. You will see people refer to the JLS, or the Java Language Specification. These are the rules and grammar of java.

what are you trying to do here with java MyClass(String "hello", int 7) ?

Well, if the method weren't static, you can only call it if you have an instance of the class. So get an instance, you have to call a constructor. A constructor can take arguments. So, you would have to allow for the creation of the class by passing in arguments. You would need to be able to identify if the '7' I passed in was supposed to be an int or a String, since you can't tell by just looking at "7".

Basically, I was trying to say "Create an instance of MyClass using the constructor that takes a String and an int, call its main() method, and then additionally pass in param1, param2 and param3 as arguments".

It got very ugly very fast.

COULD it have been done? I think so. But there is no benefit that I see.
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
Rubbal Bhusri wrote:
Please make yourself more clear, I didn't get your point.


Simply put: I see no reason why it should be non-static.

Rubbal Bhusri
Ranch Hand

Joined: Dec 30, 2010
Posts: 71
fred rosenberger wrote:
What do you mean by define the language ?


Someone makes up the rules for what is allowed and what is not. For example, someone decided that Strings would be immutable. someone decided that the main method had to be static. You will see people refer to the JLS, or the Java Language Specification. These are the rules and grammar of java.

what are you trying to do here with java MyClass(String "hello", int 7) ?

Well, if the method weren't static, you can only call it if you have an instance of the class. So get an instance, you have to call a constructor. A constructor can take arguments. So, you would have to allow for the creation of the class by passing in arguments. You would need to be able to identify if the '7' I passed in was supposed to be an int or a String, since you can't tell by just looking at "7".

Basically, I was trying to say "Create an instance of MyClass using the constructor that takes a String and an int, call its main() method, and then additionally pass in param1, param2 and param3 as arguments".

It got very ugly very fast.

COULD it have been done? I think so. But there is no benefit that I see.

Sorry fred, you made it more messy !
Its totally out from getting understand.
I am still
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4343
    
    8

Here's my (relatively simple, I hope) take on it.

To invoke a non-static method, you need an instance of the class to call it on.

So, if we make the main method non-static, we need a way of creating an object before any code is executed. Surely it's easier to not require that?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Rubbal Bhusri wrote:I don't understand your point !


This seems to be true no matter what people say.

But that just means that you don't need to know the answer to this question yet. My suggestion would be to forget it for now. Go off and learn Java. When you have done that, the answer to your question will be obvious and you won't need to ask it.
Rubbal Bhusri
Ranch Hand

Joined: Dec 30, 2010
Posts: 71
Paul Clapham wrote:
This seems to be true no matter what people say.

But that just means that you don't need to know the answer to this question yet. My suggestion would be to forget it for now. Go off and learn Java. When you have done that, the answer to your question will be obvious and you won't need to ask it.


Excuse me sir ! my interview is gonna take place next week, So, what-if they aske me the same question, because this seems to be the most basic and 1st-most thing we use in java, right ?
Therefore, I need to be clarified on this, rather than avoiding.
and I think Mathew Brown's reply seems reasonable (i.e.)
To invoke a non-static method, you need an instance of the class to call it on.

So, if we make the main method non-static, we need a way of creating an object before any code is executed. Surely it's easier to not require that?

If you(Paul Clapham) have the same view as that of Mathew Brown's , then, its ok, and if you have different answer to this then of-course you should share with me.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Rubbal Bhusri wrote:my interview is gonna take place next week, So, what-if they aske me the same question

If you get asked this question in an interview the only 'correct' answer you can give is
'Because that was how the language was designed.'
Unless you can ask the designers themselves, anyone else's answer is just a guess.
If the interviewer pushes you for a guess, then the answers you have been given here should be enough.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38041
    
  22
Rubbal Bhusri wrote: . . . Excuse me sir ! my interview is gonna take place next week, So, what-if they aske me the same question, because this seems to be the most basic and 1st-most thing we use in java, right ? . . .
Disagree.
  • 1: What they want in interviews is to see how you handle things; they may ask questions with no answer and how you respond is more important than the actual answer.
  • 2: No, it is not the most basic thing. If I were interviewing and asking about static members, I might ask why any member is static.
  • 3: Unless you have information that a particular question is to be asked, learning answers to specific questions is a poor way to prepare for an interview.
  • fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11161
        
      16

    Matthew Brown wrote:Here's my (relatively simple, I hope) take on it.

    To invoke a non-static method, you need an instance of the class to call it on.

    So, if we make the main method non-static, we need a way of creating an object before any code is executed. Surely it's easier to not require that?

    This is what i was trying (and failing) to show...

    As java is written now, you would write this to call the main() method on the MyClass class:

    java MyClasss param1 param2 param3

    This passes param1-3 into the Strings array in the main(String [] args) of your main method.


    But if main is non-static, you can only call it if you have an object. So, you need to call a constructor for the object. A constructor is the class name and a list of params...So for MyClass, there may be a constructor defined as



    So i would need to call this to create the object so I can call the main() method...

    java MyClass(String "hello", int 7), param1 param2 param3

    The blue part calls a constructor of the MyClass, giving me an object, which I could then pass in the three parameters...

    But I still don't have anyplace to store the reference to the MyClass object. An object with no references is eligible for garbage collection, so now I need to figure out how to handle that...and the whole thing has become a big, ugly, pointless mess.

    NONE of this proposal of mine is serious. It was simply a mental exercise to think about what would happen. That is not to say there couldn't be other/better/smarter ways of doing this, but really, just making the main() method static is a simple, elegant solution, IMHO.
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18541
        
        8

    Rubbal Bhusri wrote:
    Paul Clapham wrote:
    This seems to be true no matter what people say.

    But that just means that you don't need to know the answer to this question yet. My suggestion would be to forget it for now. Go off and learn Java. When you have done that, the answer to your question will be obvious and you won't need to ask it.


    Excuse me sir ! my interview is gonna take place next week, So, what-if they aske me the same question, because this seems to be the most basic and 1st-most thing we use in java, right ?
    Therefore, I need to be clarified on this, rather than avoiding.


    You've already been "clarified". Several times, in fact. So at this point my suggestion would be to hope they aren't going to ask you that question.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Why main(String[] args) -method is made static ?
     
    Similar Threads
    Reg. final keyword
    how to access static nested class w/o instance of outer class
    static method
    static methods
    non-static method can't be referenced from static context