aspose file tools*
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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java life cycle in kindergarden form?" Watch "java life cycle in kindergarden form?" New topic
Author

java life cycle in kindergarden form?

donald rieck
Ranch Hand

Joined: Mar 12, 2003
Posts: 75
create a class: app.java
compile it: javac app.java
Is this order good?
JVM loads class app.java.
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 app.java.
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
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy,
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.
cheers,
Kathy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java life cycle in kindergarden form?