2

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 ");
    }

} 
Karolis.sh
  • 1,618
  • 20
  • 20
Leonne
  • 85
  • 3
  • 10
  • 1
    possible duplicate: http://stackoverflow.com/questions/2943556/static-block-in-java – user432 Apr 29 '14 at 08:47
  • `static { }` is added to the static initialisation of the class. It is executed top to bottom when the class is initialised. – Peter Lawrey Apr 29 '14 at 08:48
  • ahh.. that explains why x is first to be printed.. thanks ya'll – Leonne Apr 29 '14 at 08:52
  • `static { ... }` is the class initializer which is called when the class gets loaded into the JVM the very first time. It is always executed before you can access the type somewhere else in your code. – Harmlezz Apr 29 '14 at 08:57
  • 1
    Absolute copy of this [question](http://stackoverflow.com/questions/13699075/calling-a-java-method-with-no-name) and should be marked duplicate – Santhosh Apr 29 '14 at 09:02

6 Answers6

3

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
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
  • Absolute copy of this [question](http://stackoverflow.com/questions/13699075/calling-a-java-method-with-no-name) and should be marked duplicate – Santhosh Apr 29 '14 at 09:09
2

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

awksp
  • 11,764
  • 4
  • 37
  • 44
1

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 "); 
} 

} 
sanbhat
  • 17,522
  • 6
  • 48
  • 64
0

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

x as the first output
Santhosh
  • 8,181
  • 4
  • 29
  • 56
0

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.

Sunny
  • 308
  • 2
  • 14
0
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

Hirak
  • 3,601
  • 1
  • 22
  • 33