1

This is the code I'm running , the output of this code is 4 2 1 3 , could someone please explain why the result was printed in this order.

public class goFuncTest {
        goFuncTest() 
        {
             System.out.print("1 ");                
        } 
        {               
             System.out.print("2 ");            
        } 
        public static void main(String[] args)
        { 

            new goFuncTest().go(); 
        } 
        void go()
        { 
             System.out.print("3 ");                
        } 
        static
        { 
            System.out.print("4 ");                 
        } 

    } 
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
Ahmad Abu Baker
  • 47
  • 1
  • 1
  • 10

4 Answers4

4

Based on your recent question edit, your output will be 4 2 1 3. The static initializers are run first, then the instance initializers. If you had multiple initializers of the same type, they would execute in the order they appear in the class.

// static initializer first
static {
  System.out.print("4 ");
}

// then instance initializer
{ 
  System.out.print("2 "); 
} 

Next the constructor fires up, which gives you:

goFuncTest() 
{
  System.out.print("1 "); 
} 

finally the method is invoked:

void go()
{ 
  System.out.print("3 "); 
} 
Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
  • Sorry it seems that my code is missing a static keyword , this is the correct code public class goFuncTest { goFuncTest() { System.out.print("1 "); } { System.out.print("2 "); } public static void main(String[] args) { new goFuncTest().go(); } void go() { System.out.print("3 "); } static { System.out.print("4 "); } } – Ahmad Abu Baker Oct 22 '14 at 10:38
  • there is a static keyword before {System.out.print("4 ")} – Ahmad Abu Baker Oct 22 '14 at 10:39
1

As JB Nizet pointed out in first comment, result should be 2,4,3,1.

And coming to the order, static blocks executes first and then initialization blocks. In your code there are no static blocks and only inti blocks.

The order of initialization block execution is the order they placed in source code -2,4.

And remaining two results as per constructor and method call. 1,3

Now you can see the answer right ?

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

Reason:

  1. You have instance block which gets executed in sequence in which appear in your class definition. So 2 comes first and then 4 and hence output 2 4
  2. Next you are calling new goFuncTest(), which is going to call your constructor and hence you will see 1 in the output.
  3. Now on your instance you are calling go method which prints 3.
SMA
  • 36,381
  • 8
  • 49
  • 73
0

It seems the main confusion is just due to not understanding the various blocks that a class can have;

this answer gives a nice example and explanation;

Here's the code they give:

public class Test {

    static int staticVariable;
    int nonStaticVariable;        

    // Static initialization block:
    // Runs once (when the class is initialized).
    static {
        System.out.println("Static initalization.");
        staticVariable = 5;
    }

    // Instance initialization block:
    // Runs before the constructor each time you instantiate an object
    {
        System.out.println("Instance initialization.");
        nonStaticVariable = 7;
    }

    public Test() {
        System.out.println("Constructor.");
    }

    public static void main(String[] args) {
        new Test();
        new Test();
    }
}

This class has a static initialiser, an instance initialisation block, a constructor, and a class method.

static initialiser: static {...} instance initialiser: {...} constructor: Public ClassName(){...} class method: Public Static Whatever classMethod(String[] args){...}

Each of these is called under different circumstances; the static initialiser is called when the class is loaded, there's no other way to call it, you can't even do it via reflection, as it's never represented by a method instance, it is called by the JVM.

the instance initialiser is called whenever you create an instance of the class - before the constructor.

You can have multiple static initialisers, and multiple instance initialisers, and they are executed in the order they appear in the code.

constructor and class method you presumably already know about.

In your example, it would probably be a little more helpful to rearrange the code slightly, to better reflect that hierarchy;

public class goFuncTest {

    //static instance initialiser
    static { 
        System.out.print("4 ");                 
    } 

    //instance initialiser
    {               
        System.out.print("2 ");            
    } 

    //Constructor
    goFuncTest(){
        System.out.print("1 ");                
    }

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


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

} 

(editted to add in the static keyword)

Community
  • 1
  • 1
will
  • 10,260
  • 6
  • 46
  • 69