Search code examples
javarubyequivalent

Is there a Java equivalent to if __FILE__ == $0?


I know that in Python and Ruby there are the snippets if __name__ == '__main__': and if __FILE__ == $0, which would run only run if the script was opened directly.

This seems like a really useful feature that I haven't seen in Java (my school's "official" programming language). Is there any equivalent to this in Java? If not, is there any way to implement it?


Solution

  • java has the public static void main(String[] args) method. this is invoked when a class is run as the main class from the command line, and generally only invoked in such a scenario (you could call it directly, but it usually doesn't make sense). so, in java, the standard is to put "main invocation" logic in this method.