Tricky Java program

Take a 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 , which means that the marker method must be executed. But there are some rules:

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

Three days have passed, and I can’t solve the problem, because I am not a Java programmer. I searched everything on the Internet to get the key, but I failed. please someone help me run this program by strictly following the rules.

+6
source share
12 answers

I think they are looking for a static initializer .

 static { marker(); } 

This starts when the class loads.

+18
source

The problem is that you don't need a method for your token - you just need a static initialization block:

 class Trick { // not a method, just something to execute when the class is loaded static { System.out.println("executed"); } public static void main(String[] args) { } } 

Results: http://ideone.com/bQAgT

If you are already provided with the code (which I'm not sure because static marker() not a valid code), you can simply call marker() from your static block.

+4
source

you can use a static initializer that will execute before the Main method runs.

 static { marker(); } 
+2
source

The static block will do the trick for you. But you have a syntax error in your program. Since marker is a method, it must have a return type. I guess void .

 class testTrick { public static void main(String args[]) { } static void marker() { System.out.println("programe executed"); } static { marker(); } } 
+2
source

It is easy. In the class, add a static constructor to call the token method, and then in the constructor placed in system.exit

+1
source

An even more complicated program -

 class testTrick { static { marker(); System.exit(0); } static void marker() { System.out.println("program executed"); } } 

Note: the token must provide a return type, for example. void or it will not compile. You can save the main () method if you want, but it is never called.

The static initializer is called before the main method, so it can call another method, and it can exit before you see a message in which the main () method is not found.

+1
source
 class testTrick { public static void main(String args[]) { } static { marker(); } static void marker() { System.out.println("programe executed"); } } 
0
source

Does this fit your rules? Creating a static block will always be performed as it is stored along with the class files, and it needs a main method to call

 class TestTrick{ public static void main(String args[]) { } static marker() { System.out.println("programe executed"); } } 
0
source
 public class M { public static void main(String[] args) { } static { marker(); } public static void marker() { System.out.println("Done"); } } 
0
source
 class TestTrick{ public static void main(String args[]) { } static{ marker();} static void marker(){ System.out.println("programe executed"); } 

Look at the requirements. You can do whatever you want until you change the methods.

Thus, a static block is required (which will always be executed in the program). This is not a method, and you can call marker() from there (to which btw should be assigned a return type)

0
source
 int i = 10 + + 11 - - 12 + + 13 - - 14 + + 15; System.out.println(i); 
-1
source

You will need to call the marker method in the main ie method; something like that:

 class testTrick { public static void main(String args[]) { marker(); } static void marker() { System.out.println("programe executed"); } } 
-2
source

Source: https://habr.com/ru/post/904081/


All Articles