2 Replies - 376 Views - Last Post: 08 August 2012 - 03:25 AM Rate Topic: -----

#1 burakaltr  Icon User is offline

  • D.I.C Regular

Reputation: 92
  • View blog
  • Posts: 276
  • Joined: 07-November 10

What is the Point of Java making void main(String args[]), static

Posted 08 August 2012 - 02:39 AM

What is the point when one cannot reach non-static values directly, unless one creates an instance ? Could not it be made non-static ?

Thank You.
Is This A Good Question/Topic? 0
  • +

Replies To: What is the Point of Java making void main(String args[]), static

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: What is the Point of Java making void main(String args[]), static

Posted 08 August 2012 - 03:21 AM

The answer is because that is what the creators of Java decided upon and since none of us were in the room when the decision ws made, the best any of us can do is postulate.

It may have been about the mental model they wanted the programmer to adopt. Perhaps it would have been better to start the program from the run method of a Runnable. That would more closely align the programmer's mental model with what actually happens within the JVM. Of course, I say that with the perspective of someone who has been coding in Java for 12 years. I have no idea what my beginner self would have thought of that suggestion.

A reasonable mental model is that at the start of your program there are no objects in existence until you create them. If no objects exist then any executable code must be static.

That's a nice simple mental model that can be explained to beginners.

This post has been edited by cfoley: 08 August 2012 - 03:23 AM

Was This Post Helpful? 3
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: What is the Point of Java making void main(String args[]), static

Posted 08 August 2012 - 03:25 AM

If main() was not static you couldn't reach it without having an instance of your class so where would be the code to create that instance so you can access it ?

> java MyClass

means execute code in MyClass.main()
If main would be non static you would have to:

String[] args = new String[0];
MyClass x = new MyClass();
x.main(args);

but you can't create a MyClass object at the console prompt

This post has been edited by pbl: 08 August 2012 - 03:38 AM

Was This Post Helpful? 3
  • +
  • -

Page 1 of 1