Question

I'm having trouble understanding the following code's execution. I want to follow the Java program so that I can understand how everything works together. I step up breakpoints in Eclipse but it doesn't explain why. Here's the code:

public class Sequence {
    public Sequence() {
        System.out.print("c ");
    }

    {
        System.out.print("y "); 
    }

    public static void main(String[] args) {
        new Sequence().go();
    }

    void go() {
        System.out.print("g ");
    }

    static {
        System.out.print("x ");
    }
}

The output to the code is x y c g. Can someone explain why this is? I thought the program entry point was public static void main but it appears static executes first?

Was it helpful?

Solution

The static block is executed before the main starts, so x get printed.

Then we enter the main, and we call

new Sequence().go();

Which calls the Sequence constructor. As per the static block, before The Sequence constructor gets called (so before a new Sequence object gets initialized), the instance block (the one written within the braces) gets executed, so y gets printed.

Then the constructor call prints c.

In the end, the go() method gets called on the newly created object, so g gets printed.

So the full output will be

x y c g

OTHER TIPS

The JLS is of help here, chapter 12 to be precise.

First the static block will run. This will happen when the class is loaded by the ClassLoader.

Then main will run, this is executed by the JVM to start the application.

Then your constructor is executed when you call new Sequence(). The compiler will yank your instance initialiser (the bit in curly brackets) into the top of the constructor (after the implicit call to to the superclass constructor). So the bit in curly brackets runs first then the code in the constructor.

Finally the method go() is called.

So the output of the code is x y c g

Java execute static block after class loading and before any method. Main method is entry point for any program but it is however a method then static class initialization.

In your class you have used

//constructor
    public Sequence() {
        System.out.print("c ");
    }

    //instance init block
    {
        System.out.print("y "); 
    }

    //method
    void go() {
        System.out.print("g ");
    }

    //static init block
    static {
        System.out.print("x ");
    }

-> Init blocks execute in the order they appear.

->Static init blocks run once, when the class is first loaded.

-> Instance init blocks run every time a class instance is created.

-> Instance init blocks run after the constructor's call to super().

->Constructor run after when you create an instance.

According all of that rules you got,as expected x y c g output

First jvm loads static blocks when application starts. So static block executed first. Then main method execution starts.

{
        System.out.print("y "); 
 }

is the constructor block it will be copied to each constructor so when u instantiate class it will be called every time. Click here

Steps:

i. When class is loaded then static block is executed first.

ii. Every time object of that class is instantiated then initialization block i.e.

 {
   System.out.print("y ");
 }

 is executed(every time) and after that the time of constructor comes to be executed.

iii. When object creation is finished then go() method is executed. And hence the output.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top