File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes java life cycle in kindergarden form? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java life cycle in kindergarden form?" Watch "java life cycle in kindergarden form?" New topic

java life cycle in kindergarden form?

donald rieck
Ranch Hand

Joined: Mar 12, 2003
Posts: 75
create a class:
compile it: javac
Is this order good?
JVM loads class
Static members are initialized.(vars,blocks,methods)
JVM goes to the main method.
Object is declared in main: App myApp;
Object creation: myApp = new App();
New is envoked.
New operator allocates memory.
Instance variables are initialized to their defaults.
New operator calls the super ctor.
New operaator calls the original ctor.
Constructor creates the "object."
Constructor body is not yet executed though.
Constructor initializes all instance variables to 0,null or false.
Constructor body is now executed.
Is this a good "loose" interpretation?
[ March 15, 2003: Message edited by: donald rieck ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I think what you need is described well enough in the Java Virtual machine Specification.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
donald rieck
Ranch Hand

Joined: Mar 12, 2003
Posts: 75
The JVM spec is very envoled for right now, I'm just looking for a simple quick confusion check.
donald rieck
Ranch Hand

Joined: Mar 12, 2003
Posts: 75
Sorry if you think I was trying to double post.
The two post are different in a small but important way.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
JVM loads class
The JVM loads class app.class! Once the .class file has been created you can delete the .java and your program will run with no trouble.

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Kathy Sierra
Cowgirl and Author

Joined: Oct 10, 2002
Posts: 1589
the only thing I wanted to clarify is with the order in which constructors are called and variables initialized.
Starting with...
* Constructor is invoked
* instance variables are given default values (0, null, false, etc.)
* super constructor is invoked
* super constructor completes
* instance variables are now give their explicit initialization values (values assigned when the variables are declared)
* constructor completes
The key scary thing in there is the Schroedinger's cat state the object is in when the super constructor is running. You cannot, for example, pass instance variables to your super constructor, or invoke instance methods as part of the call to super.
You should also avoid calling any non-final or non-private instance methods from within the constructor. Because let's say your super constructor is running, and it makes a call to a method (a method of the superclass, in other words, a method in the same class as the constructor that's now running).
Now imagine that YOU have overridden that method... your overridden version will be called, even though YOUR constructor has not yet run! In other words, polymorphism is still alive and kicking, causing a virtual method invocation of the overridden method, yet that overridden method probably uses instance variables, which have not yet received their "real" initial values!
99% of the time, that half-alive-half-not-alive state won't be noticed, as long as you don't try to call potentially overridable instance methods from a constructor.
I agree. Here's the link:
subject: java life cycle in kindergarden form?
It's not a secret anymore!