Question

pretty rusty but im pretty sure i've never seen a code written like this. it is a mock question from java associate exam could someone tell me whether the 'static' in line 10 is connected to the go() method?? and mainly why is the output is x y c g ???

public class testclass {

    testclass() {
        System.out.print("c ");
    }

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

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

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

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

} 
Was it helpful?

Solution 4

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

This is the static initializer block. This will be called at the time of class loading. Thus first call.

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

This is non static initializer block. Will be called the first thing once a object is created.

testclass() { System.out.print("c "); }

This is constructor. Will be executed in the process of object creation, after all initializer blocks are executed.

Lastly,

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

Normal method call. Last thing to be executed.

For more details, please refer http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html

OTHER TIPS

tell me whether the 'static' in line 10 is connected to the go() method??

It's not relevant to that go method. It's called as static initialization block.

why is the output is x y c g ???

Following is the order of execution in java

  1. In class loading time, static field/initialization blocks will be executed.
  2. In a object creation time, JVM sets fields to default initial values (0, false, null)
  3. Call the constructor for the object (but don't execute the body of the constructor yet)
  4. Invoke the constructor of the superclass
  5. Initialize fields using initializers and initialization blocks
  6. Execute the body of the constructor

The static block there is a static initialization block that will be run when the class is loaded.

http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

It is poorly indented code. In the above class you have

  1. Constructor
  2. A class block
  3. A static block
  4. And a method called go()

class testclass { 

/**
 * Constructor, which gets called for every new instance, after instance block
 */
testclass() { 
         System.out.print("c "); 
} 

/**
 * This is instance block which gets called for every new instance of the class
 * 
 */
{ 
  System.out.print("y "); 
} 

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

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

/**
 * This is static block which is executed when the class gets loaded
 * for the first time
 */
static { 
      System.out.print("x "); 
} 

} 

static blocks will be intialised first as the class loads. thats the reason you get the o/p as

x as the first output

It is static initialization block. So when you create an object of that class it runs first the static initialization block even before the constructor.

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