When solving Olympic problems, and not only, people often in the main method create an object of the same class, and run its already non-static run method.

public class A { public static void main(String[] args) { new A().run(); } private void run() { //code here } } 

Example from acm.timus.org FAQ

Why do this if you can just mark all members as static?

    1 answer 1

    If you implement Runnable , then in previous versions of Java this was the only way to extract a big stack and make, say, a deep recursion possible.

    And so it is trite more convenient - everywhere to write static is not only lazy, but also unsympathetic. In the code that you gave one line of code and one extra method replaces thousands of static 's.

    I personally, I use not one run , but the methods init , solve , finish , where I declare input and output in init , I do flush for the output stream in finish , and in solve I just write a solution to the problem. Thus, using templates in your favorite IDE, you can make a convenient template in such a way that all this standard routine (I / O) is not written a thousand times, and the IDE itself generated the code, and you just wrote the solution to solve .

    to Dex: who prevents you from simply making the run method, you don't need to implement Runnable for this :)

    • Yes, yes, I first wrote, then I thought. But you, it seems to me, have described everything in a somewhat confusing way. The fact is that it is possible to access from a static function only to static fields and class functions. Or create yourself as an object and work with yourself and with all your fields and methods as needed. - Dex
    • Well, people seem to understand the difference between static and non-static, why talk about it then. As I understood, the question was “Why is everyone doing this if it can be so” and it was said specifically about Olympiad programming. I explained my motivation to do it through the creation of the object. - system29a
    • hmm, cool, and if you also screw fluent interfaces, you can write new A (). init (). solve (). finish (); :) - GLAGOLA
    • 2
      "If you implement Runnable, then in previous versions of Java it was the only way to grab a big stack for yourself" - you write something strange here. The fact that a class implements any interface does not affect or affect the size of the stack. Perhaps it was about creating a stream with Runnable in the form of Main. - cy6erGn0m
    • I just meant it. - system29a