JvmShutDownHook, Copy Constructor

Every Java Program can attach a shutdown hook to JVM, i.e. piece of instructions that JVM should execute before going down. A program may require to execute some pieces of instructions when application goes down. An application may go down because of several reasons:

  • Because all of its threads have completed execution
  • Because of call to System.exit()
  • Because user hit CNTRL-C
  • System level shutdown or User Log-Off

Few scenarios where this requirement fits are:

  • Saving application state, e.g. when you exits from most of the IDEs, they remember the last view
  • Closing some database connections
  • Send a message to System Administrator that application is shutting down.

Lets see this by few example. First we just make a thread class


package mynotes.shortConcepts;

public class JvmShutDownHookThread extends Thread{

@Override
public void run() {
System.out.println("JvmShutDownHookThread called");
}

}


public static void main(String[] args) {
JvmShutDownHookThread jvmShutDownHookThread=new JvmShutDownHookThread();
Runtime.getRuntime().addShutdownHook(jvmShutDownHookThread);
System.out.println("Before Exit");
System.exit(0);
System.out.println("After Exit");
}

Output:


Before Exit
JvmShutDownHookThread called


public static void main(String[] args) {
System.out.println("Main start");
JvmShutDownHookThread jvmShutDownHookThread=new JvmShutDownHookThread();
Runtime.getRuntime().addShutdownHook(jvmShutDownHookThread);
System.out.println("Main end");
}

Output:


Main start
Main end
JvmShutDownHookThread called

Copy Constructor

A copy constructor is a constructor that takes only one argument which is of the type as the class in which the copy constructor is implemented. Copy constructors are widely used for creating a duplicates of objects , But we have to ensure that both original and duplicate objects refer to different memory locations.

As we know objects can be cloned in Java using clone() method. But copying object using clone() is not flexible and extensible, and sometime doesn’t work fine as per our requirement.

The first problem is that no constructor is called when the object is being cloned. As a result, it is your  responsibility, as a writer of the clone method, to make sure all the members have been properly set. Here is an example of where things could go wrong. Consider a class keeping track of the total number of objects of that type, using a static int member. In the constructors you would increase the count. However, if you clone the object, since no constructor is called, the count will not truly reflect the number of objects.

Further, if the class has final fields, these can’t be given a value in the clone method. This leads to problems with properly initializing the object’s final fields. If the final field is referring to some internal state of the object, then the cloned object ends up sharing the internal state and this surely is not correct for mutable objects.

When you pass an instance of a particular class to copy constructor, it returns a new instance of the same class with all values copied from the parameter instance. Let’s see to an example.


package mynotes.shortConcepts;

class Employee{
private int id;
private String name;
//Parameterized constructor
public Employee(int id,String name) {
this.id=id;
this.name=name;
}
//copy constructor
public Employee(Employee original){
this.id=original.id;
this.name=original.name;
}
@Override
public String toString() {
return "id=>"+this.id+" name=>"+this.name;
}

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}

public class CopyConstructorTest {
public static void main(String[] args) {
Employee emp1=new Employee(1,"abcd");
Employee emp2=new Employee(emp1);
System.out.println("emp1=>"+emp1);
emp1.setName("qwerty");
System.out.println("emp1=>"+emp1);
System.out.println("emp2=>"+emp2);
}

}

Output:


id=>1 name=>abcd
id=>1 name=>qwerty
id=>1 name=>abcd

Suppose Employee has a reference to a Object say Department, then we need to make sure that a copy of Department (use copy constructor in Department also) is setted to the object created by copy constructor, else both object will point to the same department object which will create issues later on.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: