Static blocks in java
Introduction
Hi everyone, it has been a while since I have put a new topic. In this blog post, I will be writing about static blocks in java. Static blocks is a block of code that get executed first when the class is loaded.
Static block will be the first to execute when a class is called. Please check the below example.
Hi everyone, it has been a while since I have put a new topic. In this blog post, I will be writing about static blocks in java. Static blocks is a block of code that get executed first when the class is loaded.
Static block will be the first to execute when a class is called. Please check the below example.
package com.blog.techjourney;
public class StaticBlockExample {
static
{
System.out.println(" Inside static block");
}
public static void main(String[] args)
{
System.out.println(" Inside main method");
}
}
public class StaticBlockExample {
static
{
System.out.println(" Inside static block");
}
public static void main(String[] args)
{
System.out.println(" Inside main method");
}
}
In the above example, the static block will get executed before the main method. The output of the above program is as follows:
Inside static block
Inside main method
Inside main method
If there are more than one static block in a class, each of the static blocks get executed before the main method. The order of execution of the static block will be the same as the order in which they are specified.
In the below example two static blocks are inside a class and both will get executed before the main class.
package com.blog.techjourney;
public class StaticBlockExample {
static
{
System.out.println(" Inside first static block");
}
public static void main(String[] args) {
System.out.println(" Inside main method");
}
static
{
System.out.println(" Inside second static block");
}
}
public class StaticBlockExample {
static
{
System.out.println(" Inside first static block");
}
public static void main(String[] args) {
System.out.println(" Inside main method");
}
static
{
System.out.println(" Inside second static block");
}
}
Here, both the static blocks will get executed before the main method. The output of the above program is as follows:
Inside first static block
Inside second static block
Inside main method
Inside second static block
Inside main method
Also static blocks gets executed before a constructor gets executed. Here is an example program showing the execution of static block before the constructor.
package com.blog.techjourney;
public class StaticBlockExample {
public static void main(String[] args) {
StaticBlocks testStaticBlock = new StaticBlocks();
}
}
class StaticBlocks
{
static
{
System.out.println(" Inside static method");
}
StaticBlocks()
{
System.out.println(" Inside constructor");
}
}
public class StaticBlockExample {
public static void main(String[] args) {
StaticBlocks testStaticBlock = new StaticBlocks();
}
}
class StaticBlocks
{
static
{
System.out.println(" Inside static method");
}
StaticBlocks()
{
System.out.println(" Inside constructor");
}
}
In the above program, the static block will get executed before the constructor. The output of the program is as below.
Inside static method
Inside constructor
Inside constructor
The main reason for using static blocks is to initialize static variables in a class. We cannot use constructors to initialize a static variable because static variables are not a part of class definition. Static blocks are also used for security related issues.
Following program defines how a static variable is initialized by a static block.
package com.blog.techjourney;
public class StaticBlockExample {
public static void main(String[] args) {
System.out.println("number = "+StaticBlocks.number);
}
}
class StaticBlocks
{
static int number;
static
{
number =17;
}
}
public class StaticBlockExample {
public static void main(String[] args) {
System.out.println("number = "+StaticBlocks.number);
}
}
class StaticBlocks
{
static int number;
static
{
number =17;
}
}
The output of the program is as below.
number = 17
Static Block in short
- Static block will get executed when the class is loaded.
- Static blocks execute before the main method.
- Static blocks also gets executed before the constructor.
- Used for initializing static variables.
- Static blocks can access only static variables
Thank you for reading the post.
Comments
Post a Comment