0

While reading about Java Programming I couldn't get why it states Java static main necessity reason is because "before you're program starts,there aren't any objects to send messages to" as said in slide 8 of static variables.

The reason why I couldn't get this is why do you need an object to execute a program. I think in C program they haven't invoked any object to perform execution of C program.

Also since machine language is a sequence of instructions why should we consider objects here when the Java bytecode is also almost an machine language.

gnat
  • 21,442
  • 29
  • 112
  • 288
justin
  • 237
  • 2
  • 12
  • 1
    Its called Object Oriented for a reason... – wam090 Nov 28 '14 at 10:42
  • That's an example of a very good newbie question. – sharptooth Nov 28 '14 at 10:50
  • @gnat: That question is about why it is a static function and not a non-static function (less newbie). This one is about why it is a member function in the first place. – sharptooth Nov 28 '14 at 10:56
  • @sharptooth what I read in the question is, "I couldn't get why it states Java static main necessity reason". As far as I can tell, duplicate question asks about this. If asker intended to ask about something else, they can [edit] to clarify – gnat Nov 28 '14 at 10:59
  • @gnat: This is a 80th level newbie question - the OP cannot get out of the following dilemma: the book says it has to be static because there's no object yet and he cannot get why they would need an object given that C and C++ don't need an object. – sharptooth Nov 28 '14 at 11:01

1 Answers1

2

The fundamental problem is that you need to have an entry point - the earliest of your code that will run once the program is started. That must be something allowed by the language of choice. Non-member functions are allowed in C and C++ but not in Java.

However static member functions are very close to non-member functions (you can simply call a public static member function of any class without instantiating an object just as you would call a non-member function) and are allowed in Java and so that's the choice for how the entry point is defined.

Yes, you could perhaps have a non-static member function being used as an entry point, but which object would it be invoked on? This could be done for example by specifying a specific class as holding an entry point function (using some attribute or program configuration file or whatever else) and the runtime would instantiate it and call the member function on the object. However this is an unnecessary complication - you can have all that with a static member function used as an entry point. Once control gets there - just instantiate whatever you want and call member functions thereof.

sharptooth
  • 4,349
  • 1
  • 27
  • 37
  • :Could you give an example of stand alone function in C. – justin Nov 28 '14 at 10:50
  • @justin: All functions are standalone (also called *free functions*) in C - `fread()`, `rand()`, `malloc()`, `main()`. In C++ functions can be *member functions* and *free functions*. – sharptooth Nov 28 '14 at 10:52
  • @justin: I think calling them *non-member functions* would make things easier. http://stackoverflow.com/a/4863328/57428 I edited the answer. – sharptooth Nov 28 '14 at 10:54
  • :Does C++ have a similar object invocation as java? – justin Nov 28 '14 at 11:06
  • @justin: Yes, mostly. You can call non-static member functions on specific objects and you can call static member functions using either a specific object or a class name. – sharptooth Nov 28 '14 at 11:15
  • :Does C++ have an entry point during it's time of execution? – justin Nov 28 '14 at 11:16
  • @justin: Yes, it uses `main()` non-member function the same way as C does. – sharptooth Nov 28 '14 at 11:19
  • :Could you tell me the difference in C and java regarding the entry point during the time of execution. – justin Nov 28 '14 at 11:50
  • @justin: C runtime invokes a non-member function `main()` and Java runtime invokes a static member function `main()` of some class that happens to contain it. – sharptooth Nov 28 '14 at 12:11
  • :That's right.Is the complexity of executing a program in C and java same regarding to the 'entry point' concept.Really I would like to know is there any use in making java main's method static compared to C language where it's main method is not static. – justin Nov 28 '14 at 12:17
  • 2
    @justin: There're no member functions in C and there're no non-member functions in Java. There's no other choice in C - non-member functions are the only choice. There is some choice in Java (all functions are member functions but they can be either static or non-static). Non-static member functions could have been used as entry points - however the design choice was made in favor of static member functions and I don't know why it was done this way but I guess it is because it is simpler. Also please note that `static` for C functions and static for Java member functions are totally different. – sharptooth Nov 28 '14 at 12:23