Forgive me for my lack of Java-lingo. In fact I'd like to find a book someday that will teach me all of the definitions of words that are mentioned for Java classes and objects, as well as processes, etc.

If the title isn't at all clear, what I mean is "what happens first when an object is created?"

Is the order Static, Transient then Constructor?

For example, consider the following code--

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package chapter_21_multimedia_practice;

public class NewClass {
    
    /*Assumption: 
     Much more performance-friendly to have static calls before any pre-initialization.*/
    static { System.out.println("Running..."); /*what if I did deserializaton here?*/}
    
    /*Field variables here...*/
    
    public static void main(String args[]){
        
        System.out.println("I think I got it to work!");
        
    }
    
    
}

--from the example, you can probably tell that I was fooling around with NetBeans and somehow opened a file that had an anonymous static block so I tested it out and realized that it worked. I never saw something like it before so I was curious.

From there I tried creating an object of this class and realized that before the constructor was called, the static block ran. Also the compiler had an easier time compiling when the static call was closer to the head of the class, which is why I made the above assumption.

My question now is... what is the order of processes executed during the instantiation of a new object?

Recommended Answers

All 4 Replies

Create two small classes and have one class call new on another class (that contains all the things you want to see) and run through the instantiation in a debugger.

Though a few versions out of date, this free online book has a lot of thorough explanations of these things and is still very relevant to most of the basics: http://www.codeguru.com/java/tij/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.