Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

while printing some statements in console without main function using static block [duplicate]

Tags:

java

static

public class Riwo{

    static {
        System.out.println("hello");
        System.exit(0);
    }


}

Err

or: Main method not found in class saky.Riwo, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
like image 231
Prithvi Deora Avatar asked Nov 19 '25 10:11

Prithvi Deora


1 Answers

There is an exception for classes that extend javafx.application.Application - you don't need to define the main method for them.

The java command can be used to launch a JavaFX application by loading a class that either has a main() method or that extends the javafx.application.Application. In the latter case, the launcher constructs an instance of the Application class, calls its init() method, and then calls the start(javafx.stage.Stage) method.

Otherwise, in order to start a Java application, the main has to be defined.

Static blocks are executed when their class is being loaded into the JVM. For a class to be loaded, there should be a trigger/launcher somewhere.

like image 68
Andrew Tobilko Avatar answered Nov 21 '25 23:11

Andrew Tobilko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!