Article View: borland.public.jbuilder.java.language
Article #8636Re: newbie question
From: Joi Ellis [TeamB]
Date: Mon, 05 Mar 2001 04:26
Date: Mon, 05 Mar 2001 04:26
34 lines
2558 bytes
2558 bytes
sandelf <sandelf@poczta.onet.pl> wrote: >> I am not sure what you are asking. Why should it *not* be >> possible to create an instance of a class inside main or any >> other method for that matter? > I mean recursive instantiation of a class inside its definition. > How it is achieved, why there is no infinitive chain of creation? Here's a mental model I created for myself. It works for me, but I don't claim it's 100% accurate. The first time your program references a class, the JVM creates a Class object and sucks the java bytecode for the class into the Class object. At this point the JVM can start making use of the class, but your program hasn't instantiated it yet. Even so, your program can now call static methods on the Class object which contains the bytecodes. Only static fields and methods are accessible this way. When you launch an app via the command "java Foo", the JVM creates a Class object to contain Foo, and loads Foo's bytecodes into it. Then it calls Foo.main() via that Class object. Foo.main() can then use 'new Foo()' to instantiate (create an instance of) Foo. Only now does the JVM possess an object of type Foo. (Remember, until now it has possessed only an object of type Class containing Foo's bytecodes.) So, this isn't at all a recursive process. It just looks that way because most Java tutorials don't mention the Class object until much too late. (At least that's true of the ones I studied. I didn't figure out most of this until I studied the source code for a Classloader.) -- Joi Ellis (TeamB) No direct email, please. For best results, post on news server newsgroups.borland.com!
Technical Details
Message-ID:
<97v4gu$o6r6@bornews.inprise.com>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!cyclone2.usenetserver.com!news-sjo.usenetserver.com!news-out.usenetserver.com!newshub2.rdc1.sfba.home.com!news.home.com!enews.sgi.com!news.xtra.co.nz!newsfeed01.tsnz.net!bornews.borland.com!not-for-mailReferences:
<3a9f5ade$1_2@dnews> <3a9f6830_2@dnews> <3aa29a70_1@dnews>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,3a5b2c9b663406b5
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-03-04 20:38:07 PST
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!cyclone2.usenetserver.com!news-sjo.usenetserver.com!news-out.usenetserver.com!newshub2.rdc1.sfba.home.com!news.home.com!enews.sgi.com!news.xtra.co.nz!newsfeed01.tsnz.net!bornews.borland.com!not-for-mail
From: "Joi Ellis [TeamB]" <joi@aravox.com>
Newsgroups: borland.public.jbuilder.java.language
Subject: Re: newbie question
Date: 5 Mar 2001 04:26:06 GMT
Organization: Another Netscape Collabra Server User
Lines: 34
Message-ID: <97v4gu$o6r6@bornews.inprise.com>
References: <3a9f5ade$1_2@dnews> <3a9f6830_2@dnews> <3aa29a70_1@dnews>
NNTP-Posting-Host: linux.aravox.com
User-Agent: tin/1.4.2-20000205 ("Possession") (UNIX) (Linux/2.2.16-3 (i686))