Hello friends,
Welcome back.
Today in this article, I'm going to explain you about the static block and initializer block. As the Java language is pure OOP language, whatever the code we write is only in classes. So whenever we use class to represent a real life entity, we use members in the class as the attributes or properties of that entity.
To initialize those properties or members, we can use one of the way i.e.
I'm assuming that you are familiar with all methods. Here about first three methods, I'm just giving a short description. The first three methods are : we initialize member in constructor by using a parameterized constructor. Setter methods is a java standard which denotes that class's member should be private and it should have setter method so that the member can be initialized. Third method is inline i.e. when we declare a member or variable, at the same time, we set its value.
In this article, we're going to discuss about the last method of initialization, the initializer blocks. Java provides initializer block mechanism which is helpful in initializing multiple members at the same time. There are two types of initializer blocks in java:
class ClassName {
public static int i;
static {
i = 10;
}
}
class ClassName {
public int i;
{
i = 10;
}
}
That's all about initialization blocks in Java.
In this article, I didn't used gist for showing syntax, because the syntax is very simple, so I thought no need to use gist. I usually use gist whenever syntax highlighting is recommended.
Let me know if anything is missing or needs to be more cleared.
Thanks.
Welcome back.
Today in this article, I'm going to explain you about the static block and initializer block. As the Java language is pure OOP language, whatever the code we write is only in classes. So whenever we use class to represent a real life entity, we use members in the class as the attributes or properties of that entity.
To initialize those properties or members, we can use one of the way i.e.
- Constructor
- Setter method
- Inlinie
- Blocks
I'm assuming that you are familiar with all methods. Here about first three methods, I'm just giving a short description. The first three methods are : we initialize member in constructor by using a parameterized constructor. Setter methods is a java standard which denotes that class's member should be private and it should have setter method so that the member can be initialized. Third method is inline i.e. when we declare a member or variable, at the same time, we set its value.
In this article, we're going to discuss about the last method of initialization, the initializer blocks. Java provides initializer block mechanism which is helpful in initializing multiple members at the same time. There are two types of initializer blocks in java:
- Static block
- Instance block
Static block:
The static blocks in java denotes its specific role in its name, yes to initialize static variables and members. These blocks are used only to initialize static members. We cannot use the this and super keyword as both keywords are instance level keywords. The static block execute only once when the class is loaded into the memory by class loader. We can create a local variable or local object in the static block but as it declared, its scope would be limited to the static block. We also cannot use instance variables and instance members of the same class in which its declared, and also we cannot call non-static methods in that block. The main purpose of this block is to initialize only static variables and members, and execute some code when class is being loaded into the memory. This block is same as any ordinary block in methods, except its written in a class and the static keyword is used. Syntax:class ClassName {
public static int i;
static {
i = 10;
}
}
Instance block:
As the name suggest and you guys would guessed, the instance block is used to initialize only non-static (i.e. instance) members and variables. We can use the this and super keyword in this block. The instance block executes every time whenever we create an object of that class. We can create a local variable or local object in the instance block, but its scope would be limited to that block only. We can use both instance and static members and methods into this block. The main purpose of this block is to initialize non-static members and execute piece of code every time whenever object is created. This block is same as a simple inline block in method, except its written in in a class. We don't need to use any keyword, syntax as follows:class ClassName {
public int i;
{
i = 10;
}
}
That's all about initialization blocks in Java.
In this article, I didn't used gist for showing syntax, because the syntax is very simple, so I thought no need to use gist. I usually use gist whenever syntax highlighting is recommended.
Let me know if anything is missing or needs to be more cleared.
Thanks.
Comments
Post a Comment
Want to share something? Add here...