I have declared three static methods in my java program, apart from the main()
method. Since a static method is executed first when a program is run, which
method will be executed first? Will it be one of the static methods that I have
declared, or will it be the main method?
The main
method will be executed first, unless you explicitly execute one of the other static methods in a static initializer block or in the initialization of a static variable.
For example, here's a scenario in which static methods (method1
and method2
) are executed before the main
method :
public class SomeClass
{
static int v = method2 ();
static {
method1 ();
}
public static void main (String[] args)
{
}
public static void method1 ()
{
}
public static int method2 ()
{
return 5;
}
}