Sitemap

All you need to know about Initializing Fields in Java

3 min readJun 3, 2023

--

Greetings Friends,

I’m Z3R0_66 a Software Engineering Student. This blog is for the Java Developers who want to explore the Java in depth. I was reading about the static blocks today and my curious mind made me do some experiments after reading a few statements. I would like to share those experiments with my fellows, so without further ado. Let’s get started.

We all know about initializing the fields in Java but before moving to initialization I want to clear somethings for beginners.

There are three types of variables you can define inside a Java Class.

  1. Instance Variables.
  2. Static Variables or Class Variables.
  3. Local Variables or I call them Scoped Variables.

Let’s Focus on the Initialization of Class and Instance Variables. We know that Instance Variable are usually initialized via a constructor right? so If you have a class Student with Id and name as instance variable you’ll either use the Constructor to initialize the value or you’ll use the setters.

public class Student{ 

// Instance Variables
private Integer studentId;
private String studentName;

// Fancy No-Arg Constuctor
public Student() {

}

// 2-Arg Constructor
public Student(Integer studentId, String studentName) {
this.studentId = studentId;
this.studentName = studentName;
}

// Getters and Setters
public Integer getStudentId() {
return studentId;
}

public void setStudentId(Integer studentId) {
this.studentId = studentId;
}

public String getStudentName() {
return studentName;
}

public void setStudentName(String studentName) {
this.studentName = studentName;
}

// Other Class Method Like toString, equals, ...
}

But Guys do you know there is an alternative to initialize the Instance Variables? Yeah you guess it right you’ll directly provide the value to instance variable at declaration time but do you know there are other alternatives?

Approach 1:

Get Z3R0_66’s stories in your inbox

Join Medium for free to get updates from this writer.

To Initialize an Instance Variable you can use something that is called initialization block. The initialization block is same as static block however the only difference is that initialization block doesn’t have static keyword before it. So the question is when the initialization block is executed? It is executed right before the constructor and after static blocks. So you can use the initialization block to set default values to the instance variables.

  1. Initialization blocks can be for something that you want to execute before object creation.
  2. Initializer block is copied inside every constructor by Java Compiler so this approach can be used to share block of code between multiple constructors.
  3. You can define more than 1 initializer blocks in a class and the order of execution will be the order in which they appear in source code.
  4. Initializer Block can be at the bottom but will be executed right before constructor.

Approach 2:

You can use the Method instance method or static method to Initialize the value to Instance Variable.

Now that we have learnt about the instance variables and initialization block let’s move to static variables and static blocks.

There are some gotchas about Static Blocks

  1. Static Blocks are for variable initializations not variable declarations. You can define a variable inside static block but it’s not accessible outside in any way.
  2. Access Modifiers (protected, private, public) are not allowed inside static block.
  3. Static Block may have final variables.
  4. Instance Variables can’t be initialized inside static block however you can Initialize the value to instance variables If you have object of class defined inside static block. Remember only in this case the constructor will be executed before the static block due to the fact of line by line execution of code but in actual static block is called first always
  5. static Blocks are always called before constructor and are only called once.
  6. static blocks can be anywhere in the code and there can be multiple static blocks just like initialization block.
  7. The alternative to static block is private static Methods since private static Methods are private to a class they are only available inside that class just like static block and they will also be used to initialize the static variable.

If you like the knowledge please clap on the blog and share your ideas.

Thanks

--

--