Tuesday, March 17

Creating Immutable Class

How to create immutable class

Immutable Classes are those who’s object once created it can’t be change (or value can’t change). So to create Immutable Class in java there are some simple that are listed below.

Rules for immutable class:


  • No setter should provide for the field.
  • All field should be final and private.
  • Make the class final as an immutable class should not be inherited or an another for this could be to make the constructor private and construct instances in factory methods.
  • If there is any field which is a references to mutable objects make sure that it should not be modified :

  • Don’t provide methods that modify the mutable objects.
  • Don’t share references to the mutable objects. Always create a copy of the object and provide that for any operation.

Sample of Immutable Class

import java.util.Date;

public final class ImmutableSample
{

    private final int intField; // Is value type 
    private final String stringField;// Is Immutable
    private final Date dateField;// Is mutable

    private ImmutableSample(int intField, String stringField, Date dateField){
        this.intField = intField;
        this.stringField = stringField;
        this.dateField = new Date(dateField.getTime());
    }


    public static ImmutableSample getInstance(int intField, String stringField, Date dateField){
        return new ImmutableSample(fld1, fld2, date);
    }

    //Provide no setter methods

    /**
    * int is value type variable, So the value in intField going change every 
    * */
    public int getIntField() {
        return intField;
    }

    /**
    * String class is also immutable so we can return the instance variable as it is
    * */
    public String getStringField() {
        return stringField;
    }

    /**
    * As Date field is mutable so we create a copy of dateField. So that modification will not effect dateField.
    * */
    public Date getDateField() {
        return new Date(dateField.getTime());
    }

    @Override
    public String toString() {
        return intField +" - "+ stringField +" - "+ dateField;
    }
}

Some advantage of Immutable Object

  • They are thread-safe so don’t have and synchronization issues.
  • They can be best use in Map’s key and Set’s element.
  • It easier to parallelize your program as there are no conflicts among immutable objects.
  • They have their class invariant established once upon construction, and it never needs to be checked again

Sunday, March 8

Builder pattern for creating object : Creational Pattern

There are number of creational patterns which are use to create instances of class, but were builder pattern fit in and stand out from other creational pattern like static factory, telescoping constructor, javabean pattern and other's. Is its ability to scale with a large number of parameters. So let's get started with an example.

Thursday, February 19

Increase JVM Size In WebSphere Application Server (WAS)

How to increase JVM Memory Size in WebSphere Application Server (WAS).

Server getting very slow and hanging up a lot sometime ending up by "java.lang.OutOfMemory" error frequently while deploying or republishing J2EE Web application in WebSphere Application Server.

Today, I'll telling how to set initial heap size and maximum heap size of JVM. that can solve these issues So to do so start your WAS first. then After the server has been started run the admin console from the IDE I am using IBM RSA(Rational Software Architect for WebSphere) you could be use whatever you like.
  1. Or the other way is, in your web browser open 'http://localhost:9060/ibm/console/'  9060 is the default port for WAS WebConsole.
    Open  this URL in web browser

Monday, September 8

Why Iterator interface provides remove() method but no add() method?

The Interface Iterator provider as three method that are hasNext(), next() and remove(). 
This means that we can remove any element from a collection while iterating over it. so why we cannot add element to the collection. Why they don't provide add() method.

Answer is, that they can't allow you to concurrently read and add element to a collection. The Iterator work on any collection whether it Set, List or Map. So it does know very much the on which underlying it's going to work and we know all the collection implementation maintain ordering of elements based on some algorithm. For example  TreeSet maintains the order of elements in by implementation Red-Black Tree data structure. Therefore if we tried to add an element to TreeSet using the iterator at given index or position of iterator. it might corrupt the state of the underlying data structure. So, the add()method could change structural state of a data structure. 

Thursday, August 7

Difference between ClassNotFoundException vs NoClassDefFoundError

ClassNotFoundException Vs NoClassDefFoundError


ClassNotFoundException :  ClassNotFoundException occurs when class loader could not find the required class in class path. So, basically you should check your class path and add the class in the classpath.

NoClassDefFoundError : This is more difficult to debug and find the reason. This is thrown when at compile time the required classes are present , but at run time the classes are changed  or removed or class's static initializes threw exceptions. It means the class which is getting loaded is present in classpath , but one of the classes which are required by this class , are either removed or failed to load by compiler .So you should see the classes which are dependent on this class.

Tuesday, August 5

Why Map dosen't implement Collection Interface?

Why doesn't Map extend Collection?

Reson 1)

Collection assume elements of one value. Map assumes entries of key/value pairs. They could have been engineered to re-use the same common interface however some methods they implement are incompatible e.g.
Collection.remove(Object) - removes an element.
Map.remove(Object) - removes by key, not by entry.

You could model a Map as a collection of entries, which is what Map.entrySet() does.

There are some methods in common; size(), isEmpty(), clear(), putAll/addAll() but these are unlikely to have much value as a stand alone interface. (Again Map.entrySet() can be used instead)

 

 Reson 2)

This was by design. We feel that mappings are not collections and collections are not mappings. Thus, it makes little sense for Map to extend the Collection interface (or vice versa).

Monday, August 4

Concept of Serialization

 What is Serialization?

Serialization is an API for encoding an objects as byte-stream and reconstructing objects from their byte-stream encodings. The serializing is process of encoding objects as byte-stream and reverse process is called deserializing.

Implementation Of Serializable

Implementation Serializable to an object allow's class instances to be serialized can be as simple as adding the words 'implements Serializable' to its declaration.

Ads Inside Post