Saturday, October 29, 2016

How to create a Singleton class

Q)What is a singleton class?

A)In simple words,  A singleton class is a class which can have only one instance at any point of time throughout the application and provides a global point of access to its instance.

Q)Ok, How can you create a singleton class?

A)
class Singleton {
       public static final Singleton INSTANCE = new Singleton();

       private Singleton() {
       }
}

                 This is a simple example for a singleton class where I used a public static final INSTANCE variable and

Friday, July 15, 2016

Tutorial on Serialization in Java - Part3

How to customize Serialization:

Anyhow, Java handles serialization process then what’s the need of customizing it. Let’s look at a small scenario.

Let’s consider our TestObject class has a transient field so that it cannot get serialized and imagine we do not have the source code for this class and we have a requirement that we need to get this field serialized which has defined as transient in the source code like below.

Friday, June 17, 2016

Tutorial on Serialization in Java - Part2

Transient keyword:

When we have a class which implements Serializable interface, that means its eligible for getting serialized but we want few fields not to get serialized then we can place the transient keyword before their declarations to tell the serialization mechanism that the field should not be saved along with the rest of that object's state. Then those transient fields will not be serialized. That means their values will not be saved into bytestream while serialization and when deserialization, those transient fields will be stored back into the object with their default values.

For example, 

Tutorial on Serialization in Java - Part1

What is serialization and how its performed?

Serialization is a mechanism through which we can save the state of an object by converting the object into a byte stream.

Serialized object (byte stream) can be transferred over network, persisted or saved into file/database.
We can deserialize the Serialized object (byte stream) and gets back the original object.

Let’s try to write a Java' program to serialize an object.