It is possible to define a block as static, which is known as static block. tatic block is used to initialize static variables. Static block executes when class file(byte code) is loaded in memory.
It is possible that we can define multiple static blocks in a class.
class StaticExample3 { static int x; static String str; static { x=100; str="Silan Software"; } public static void main(String[] args) { System.out.println("the value of x is:"+x); System.out.println("the value of str is:"+str); } }
class StaticExample4 { static int x; static String str; static { System.out.println("In static blcok-1"); x=100; str="Silan Software"; } static { System.out.println("In static block-2"); x=200; str="JAVA means Silan Software"; } public static void main(String[] args) { System.out.println("the value of x is:"+x); System.out.println("the value of str is:"+str); } }
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc