Skip to main content

Initialization blocks in Java

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.

  • 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:

  1. Static block
  2. Instance block
Both the above block's common purpose is to initialize members or variables declared in the class and execute some code if needed. But there are differences too. So instead of writing differences directly, here I'm explaining both, so you'll get the differences after reading those carefully.

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

Popular posts from this blog

Import multiple db files from folder into mysql db

Hello friends, Welcome back In the previous article, we've seen how to export database by table files, not the entire db file. Now in this article, here we're going to see the sample code for importing multiple sql files into same db. This also has been achieved by using a simple shell script. In this script, applied a for loop on list of files in a directory and by using that file name, mysql's import command is executed. The code is simple, you'll learn in just few minutes. Linux shell script: Execute above code in linux terminal to import all files into the given db. Hope this would help you. Thanks.

The final keyword in java

Hello friends, Welcome back 🙂. In this article, I'm going to give a short introduction to final keyword which would be helpful to know more about it. The final keyword in java has multiple uses. The main purpose of the final keyword is to add some restrictions on java elements (class, member and method). The restrictions have been described as below. Use of final keyword: 1. The final class : We can say the class is final when it declared as final. The restriction on final class is that the final class cannot be extended by any class. Rest of the behavior of the class remains same as any normal class. 2. The final method : The method declared using final keyword is to be said as final method. The restriction is that the final method cannot be overridden by derived class. This restriction applies to static method also. 3. The final member : The final member is the java class's member which is declared using final keyword. The restriction is that it can be initial...

XML tools on linux

Hello friends, Welcome back 🙂 In this article, we're going through XML tools which are useful for manipulation of XML. Recently I've worked on 3 tools so for now, adding those 3 tool's information, but as I get more, I'll update the article. These tools useful for validation and split/merge the XML. These tools available on both RHEL family and Debian family  linux which are same in use for both debian and RHEL. The tools are: 1. xmllint: This tool is used for xml validation. For some reason, we need to validate XML tags with its proper openings and closing i.e. syntactic validation, also we need to validate the data in the XML. The data can be different by its type which is defined in the XML's DTD, to detect this also, xmllint is useful. With this tool, there are multiple options we can use i.e. only syntactic validation, schema validation, DTD validation etc. When I was facing issue for validating the XML which having schema, I found this tool. H...