Here is a Java Program to Demonstrate static variables, methods, and blocks.
Output of Above Java Program
Static block initialized.
x = 42
a = 3
b = 12
class UseStatic { static int a = 3; static int b; static void meth(int x) { System.out.println("x = " + x); System.out.println("a = " + a); System.out.println("b = " + b); } static { System.out.println("Static block initialized."); b = a * 4; } public static void main(String args[]) { meth(42); } }
Output of Above Java Program
Static block initialized.
x = 42
a = 3
b = 12