Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java Static Block

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.

Example1;

StaticExample3.java
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);
	}
}

Output

static variable

Example2;

StaticExample4.java
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);
	}
}

Output

static variable

About the Author



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





 PreviousNext