Java programming language offers a block known as static which is executed before main method executes.
ex.
ex.
class Staticblock { public static void main(String[] args) { System.out.println("Main method"); } static { System.out.println("Static block is executed before main method."); } }
Static block can be used to check conditions before execution of main begin, Suppose we have developed an application which runs only on Windows operating system then we need to check what operating system is installed on user machine. In our java code we check what operating system user is using if user is using operating system other than "Windows" then the program terminates.
example.
class Staticblock{
public static void main(String args[]){
System.out.println("you are using window os");
}
static{
String os = System.getenv("OS");
if(os.equals("Windows")!=true){
System.exit(1);
}
}
}
ReplyDeletemain method is require... either is static block or not...