Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tricky Java program

Tags:

java

Look at the code below and please help me solve the trick.

class TestTrick{

    public static void main(String args[])
    {

    }

    static marker()
    {

    System.out.println("programe executed");

    }

}

The result required from this program is that the program should print program executed, meaning that the marker method should be executed. But there are some rules:

  1. Nothing should be written in both the methods.
  2. No other class can be added to the program.
  3. The program must execute the output statement in the marker method.

It's been three days and I am unable to solve the problem because I am not a Java programmer. I have searched everything on internet to get a clue but I failed. Please someone help me run this program by strictly following the rules.

like image 591
sum2000 Avatar asked Dec 19 '11 14:12

sum2000


People also ask

What will be the output Java?

Explanation: The output of the Java compiler is bytecode, which leads to the security and portability of the Java code. It is a highly developed set of instructions that are designed to be executed by the Java runtime system known as Java Virtual Machine (JVM).


1 Answers

I think what they're looking for is a static initializer.

static {
    marker();
}

This gets run when the class is loaded.

like image 73
Mark Peters Avatar answered Sep 30 '22 23:09

Mark Peters