why JVM is designed in such a manner?

hi all,

i m a new user to this community . I ve been wrkin on java for last one year or so. I ve done MCA ... this is all bout me.

I wanna ask a very elementary question that why do we need a

Public Static void main(String args[]) to indicate starting point of java program?

what i exactly want to ask is cant v modify JVM that we can avoid this statement

for example cant v create an instance of the class and then call its method directly.

If yes we can then why this has not been done?

If no then why we cant?

Thanx in advance

regards

kk

[610 byte] By [kaushal_kka] at [2007-9-29 16:22:54]
# 1
Maybe reason is that Java is compiled language and all known to me compiled languages need something like "entry point" to start execution. In case of interpreted languages like PERL we can start "imediate session" and issue single statements. Am I wrong ?
akulinska at 2007-7-15 14:40:00 > top of Java-index,Archived Forums,Java Programming...
# 2
may b u r right my frenbut the question is not only the entry pointqustion include why to make main method public and statici repeat cant JVM b modified to remove this.....regardskk
kaushal_kka at 2007-7-15 14:40:00 > top of Java-index,Archived Forums,Java Programming...
# 3

kaushal_kk >>

Java is an object oriented imperative language that is why it should have an entry ( it should be unique )

static = called and usable outside an object Class

--

FurtherMore you have to know that in applets

main is not the standard entry but init() and paint(Graphics)

--

This is not a problem we don't need to modify JVM for such a like reason

Nightman150a at 2007-7-15 14:40:00 > top of Java-index,Archived Forums,Java Programming...
# 4

Now I'm little bit confused:

1. if it is not static JVM must create instance first: which constructor to use ?

2. If it is not public it cannot be accessible by not-related classes as JVM classes

I belive reason of pubic static is to keep language consistent and provide entry point. Of course JVM designers can define that there is special key word eg ENTRYPOINT or just main without modifiers. But then it becomes not consistent with rest of language.

I believe JVM just loads Class (as you can in your programm) and looks for signature of public static void main(String[] a) and calls it. So JVM doesn't need to use anything but standard language features.

akulinska at 2007-7-15 14:40:00 > top of Java-index,Archived Forums,Java Programming...
# 5

JVM can know which class to b

loaded,linked n initialized from the command-

?>>> java <classname> <arguments>

Only thing it has to do after that is to create the

instance of that class n then call the main method

without static tag.?Internal code in the JVM will

look like(NO matter which language it is

implemented)...

?Object obj = new Classname();

?((Classname)obj).main(arguments);

NO problem i think !!!

?

n regarding issues such as.. what will be the scope

of such instance & how long will it?occupy the heap

space?.....These things will apply to the instance of

that class in the same way JVM treats other instances.

So the question still remains ..is it POSSIBLE?to

modify the JVM ? if YES then we r on the right

track with our doubts .....

n if NO then what is the actual reason that JVM can

not b modified in such a way?

,,,,,,,,,,,,,,,,,,,

regards

kk

kaushal_kka at 2007-7-15 14:40:00 > top of Java-index,Archived Forums,Java Programming...
# 6

Thus you have entrypoint class instead of entrypoint method.

Besides you force your class to have a default constructor which is not always desired by the programmer and this constructor has to be public.

Also you change almost nothing because the call is like

Class entryClass = Class.forName(className);

Class argumentTypes[] = {...}; //Can't remember how to indicate String[] class

Method mainMethod = _testClass.getMethod("main", argumentTypes);

mainMethod.invoke(null, arguments);

Mike

bellyrippera at 2007-7-15 14:40:00 > top of Java-index,Archived Forums,Java Programming...
# 7
oops, the "_testClass" should be "mainClass";)Mike
bellyrippera at 2007-7-15 14:40:00 > top of Java-index,Archived Forums,Java Programming...
# 8
hey belly..i think u have not understood my question properly do mail back if i m not clear enough....regardskk
kaushal_kka at 2007-7-15 14:40:00 > top of Java-index,Archived Forums,Java Programming...
# 9

If it's so goddamned important to you not to have a main() method, then just create a C program with a jvm embedded inside it (just as browsers do it), and choose some other convention for starting it up.

Personally, I'm not convinced that your idea (ultimately, requiring that classes that will be called from the command line to define a constructor that takes String[] as its argument) is any easier than using a main() method.

What a silly thing to worry about.

paulcwa at 2007-7-15 14:40:00 > top of Java-index,Archived Forums,Java Programming...
# 10

its not the matter of getting annoyed

i m more interested in knowing that why the architecture is designed in such a way .

This is just to understand it in more appropriate manner.

And just because some one did it ..... one should accept the things ... (this is what i feel)

The main focus is n the issue can JVM be modified

If yes or no - its draw backs and advantages that we can explore

any way thanx

regards

kk

kaushal_kka at 2007-7-15 14:40:00 > top of Java-index,Archived Forums,Java Programming...
# 11

I fail to see what is wrong with the static method. Being static means the class is instansiated when the programmer dictates and not be the JVM.

If you'd want the entry point to be on an actual instance of an object, then you run into problems like, when should the object be instanceated, and where is the reference (pointer) to that object held and by whom? Every instance in the runtime has a reference held for it somewhere in the programs memory space. This pointer(for want of a better word) is the latch that makes the object 'reachable' for things like garbage collection.

If you start your app with an instance, who is responsible for both maintaining the reference to the inital object and clearing it down once its finished. Sure its not impossible, but using a static entry point avoids all that. The runtime simple has to load the class and call the method, without creating any objects.

i.e. The runtime is not concerned with any form of memory managment regarding the starting of the application.

The same principle is used in C++ and it works well there. In that enviroment the memory managment is soley the responsability of the programmer so it hightlights my point better. who or what would run the 'malloc' to make space for the intially object and when would that memory be released? The system simple can not tell when you want to get rid of the intial object. With static the problem goes away, as there is no memory allocation in the first place.

eurozulua at 2007-7-15 14:40:00 > top of Java-index,Archived Forums,Java Programming...
# 12

In my opinion you got enough arguments to understand why. And no one was giving answer: "Because someone do it like this".

Probably you are waiting for answer you're expecting - so please post this answer here and we agree or not with you.

In my opinion consistency of language is good enough reason to agree at main().

akulinska at 2007-7-15 14:40:00 > top of Java-index,Archived Forums,Java Programming...