简体   繁体   中英

Java - make a method run only ONCE upon initial startup

I'm working on a program that will include a method startup() for user configuration purposes. I only want this method to run once, upon initial startup of the program. how would I go about doing this?

You can use static initializer block.

public class MyClass {
static {
 // code to be executed once on startup
 }
}

Such static initializer blocks are executed when the class is loaded, even before constructor or main method is called.

You can have any number of these blocks and they will be executed in the order they appear in the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM