• Home
  • About

Snippet IT

IT News, Programming, Internet and Blogging

  • Programming and Scripting
  • Tips and Tricks
  • Search Engine Optimization and Internet Marketing
  • Earning with Website and Blogging
  • Software and Hardware
  • New and Happening
  • Other
  • Sponsored Post
You are here: Home / Programming and Scripting / Java: Static Initializer (Static Constructor)

Java: Static Initializer (Static Constructor)

February 10, 2014 by Sze Hau Leave a Comment

Many Java programmers, including my self, might have mistaken that the portion of code that initializes the static variables of a class called the static constructor.

Why Not A Java Static Constructor?

It is actually not a constructor that initializes the static variables’ value. This is because static variables do not need to and can not be constructed as they are being shared among the instances (object) of the class. The static variables just need to be initialized when the class is first accessed by any code by the Java runtime.

What Is Static Initialization Block?

In Java, it is called “static initialization block”:

A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the static keyword. A class can have any number of static initialization blocks, and they can appear anywhere in the class body. The runtime system guarantees that static initialization blocks are called in the order that they appear in the source code.

Alternatively, you can write a private static method that return values to the static variables. The advantage of private static methods is that they can be reused later if you need to reinitialize the class variable.

Things To Take Note

Static initialization blocks are called in the order that they appear in the source code. You must be careful not to access to the variables that have not been initialized in the static initialization blocks. If you do so, you may encounter a NullPointerException during runtime. It is always painful and time wasting to trace this kind of exception.

[java]public class Example1 {
static Obj obj1;
static Obj obj2;
static Obj obj3;
static {
// First call
obj1 = new Obj();

obj2.something() // This will throw NullPointerException
}
static {
// Second call
obj2 = new Obj();
}
static {
// Third call
obj3 = new Obj();
}
}[/java]

Secondly, the alternative to static blocks, the private static method, are called in the sequence of the static variables being written in the source code.

[java]public class Example2 {
static Obj obj1 = initObj1();
static Obj obj2 = initObj2();
static Obj obj3 = initObj3();;
private static Obj initObj3() {
// Third call
}
private static Obj initObj2() {
// Second call
}
private static Obj initObj1() {
// First call
}
}[/java]

More from my site

  • Java: Static Variables, Static Methods and Static ConstructorJava: Static Variables, Static Methods and Static Constructor
  • Java: How To Create A Simple Web Server Using HttpServerJava: How To Create A Simple Web Server Using HttpServer
  • Java Static ConstructorJava Static Constructor
  • Java: Use ByteBuffer As InputStreamJava: Use ByteBuffer As InputStream
  • Java: Format Integer Into Fixed Width StringJava: Format Integer Into Fixed Width String
  • Java: Continuously Read Data From FileChannel Without MappedByteBufferJava: Continuously Read Data From FileChannel Without MappedByteBuffer

Filed Under: Programming and Scripting Tagged With: Java, static constructor, static initializer

About Sze Hau

Geek. Love programming. Coffee addicted. Married with two children. Working towards financial freedom.

Leave a Reply Cancel reply

Advertisement

  • Facebook
  • Google+
  • Instagram
  • Twitter

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

Search Engine Optimization

Short URL: Top 5 Websites That Provide Free Short URL Service

December 10, 2009 By Sze Hau Leave a Comment

Google Chromium OS

November 23, 2009 By Sze Hau Leave a Comment

WordPress Plugin: Official StatCounter Plugin

November 13, 2009 By Sze Hau 1 Comment

Make Money With Website And Blogging

Wordpress

WordPress: How To Change Admin Username

September 23, 2015 By Sze Hau Leave a Comment

WordPress: Transfer Your Blogs To Another Hosting With Minimum Downtime

February 3, 2014 By Sze Hau Leave a Comment

Virtualmin: How To Install Virtualmin in CentOS

August 26, 2013 By Sze Hau Leave a Comment

Software and Hardware

iOS 5.1 Unable to Update

March 13, 2012 By Sze Hau Leave a Comment

Blog Network

  • All Gadget Latest Gadget Reviews and News
  • Blog Portal A place where bloggers gather
  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • szehau's weblog Life, Internet, Software, Gadgets, Programming and Investments
  • Vista Talks Software News, Software Updates, Tips And Tricks

Snippet IT

This is the place where I want to share anything about information technology.

Search

Recent

  • MD5 and SHA1 Checksum Using Windows
  • MD5 and SHA1 Checksum Using Linux
  • Java: Unlimited Strength Jurisdiction Policy
  • WordPress: How To Change Admin Username
  • Linux: How To Compress And Decompress Folders And Files

Tags

Adsense advertisement advertising apache blog blogging tips C# EGPC error estimation format format Integer Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP internet marketing Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL SQL static constructor String tiny URL Tips and Tricks twitter video Windows Vista Wordpress wordpress plugin wordpress theme Youtube

Copyright © 2021 · Magazine Pro Theme on Genesis Framework · WordPress · Log in