Thursday, June 2, 2011

Java Initialization order

When a java class get loaded first all the static fields will be loaded with their default values.
Then static inits of the class begins in order, which means static fields will be initialized as given in order.


  • So in the given example code line 1 will load the class LakmaliClass with all its static fields assigning to default values.
  • Then static inits will begin executing line 5, 6 and 7.
  • Then instance inits will begin executing line 2, 3 and 4.
  • Line 2 will load the class Second with all the static fields default values (Here no static fields)
  • Then object will be created with passed values.
  • Line 3 will also do the same.



class Initializer{

    public static void main(String args[]){
        LakmaliClass o=new LakmaliClass(); // line 1
      
    }

}

class LakmaliClass{


    {
        System.out.println("Lakmali class instance init"); //line 2
        Second s1=new Second(s);            //line 3
        Second s2=new Second(number1);            //line4
    }

    static String text1="text1"; //line 5
    static int number1=1;        //line 6

    static{
        System.out.println("Lakmali class static init");    //line 7
    }
}

class Second{

    Second(String s){
        System.out.println(s);

    }

    Second(int i){
        System.out.println(i);
    }


}


result: 

Lakmali class static init
Lakmali class instance init
text1
1


Let's see a different example, where instance will be created in static init.

class Initializer{

    public static void main(String args[]){
        LakmaliClass.getInstance(); // line 1
       
    }

}

class LakmaliClass{

    {
        System.out.println("Lakmali class instance init"); //line 2
        Second s=new Second(text1,number1);            //line3
    }

    static LakmaliClass o=new LakmaliClass();            //line 4

    static String text1="text1"; //line 5
    static int number1=1;        //line 6
   
    static{
        System.out.println("Lakmali class static init");    //line 7
    }

    static LakmaliClass getInstance(){
        return o;
    }
}

class Second{

    Second(String s, int i){
        System.out.println("value1: "+s+"\nvalue2: "+i);
    }


}


result:

Lakmali class instance init
value1: null
value2: 0
Lakmali class static init


Here you can see that instance inits had run before static init. It happend in this order.

  • line 1 load class LakmaliClass with all its static fields default values.
  • Static inits begin.
  • First line 4 runs. It creates an instance of LakmaliClass. So instance init of LakmaliClass will be executed with current values. So text1 or number1 fields are not initialized to given values as line 5 and 6 have not executed yet. So text1 and number1 will have their default values.
  • Now line 5, line 6 and line 7 will execute in order.
  • Then getInstance method will be invoked.

Tuesday, May 24, 2011

Spring at a glance

Spring

Spring is an open source framework created to address the complexity of enterprise application development.

Advantages of Spring framework

Spring is a layered architecture. We can use what is required from those and leave others.

Spring enables POJO programming. POJO facilitates continuous integration, easier upgrades and switching.

The IOC feature of Spring simplifies JDBC

Open source framework.


POJO -Plain Old Java Object


Maintaining an enterprise java application for few years is even difficult to imagine due to their tightly coupled infrastructure frameworks which evolves rapidly.

Solution is using POJO. There is no magic in POJOs. There the classes don't implement infrastructure framework-specific interfaces. They are just plain Java classes. Therefore POJO decouples applications from changing frameworks. As a result, upgrading or switching to a new framework and integration will be easier and less risky.


Inversion of Control (IOC)


The concept of IOC pattern is that objects are not created,but described how they should be created. Here you do not connect your components and  services together on the code, but describe which services are needed by which components in a configuration file. In Spring, IOC container handles the hooking up all these. 
In Spring there are two types of IOC.

1. Constructor injection- Dependencies are provided as constructor parameters.
2. Setter Injection- Dependencies are given through javabean properties which are setter methods.


IOC benefits
  • Services can be easily connected by adding a new constructor or a setter method with less configuration.This minimizes the amount of code in an application.
  • IOC allows manually injecting any objects needed into the object under test. This makes unit testing more easy.
  • Loosely coupling is promoted as the dependencies are injected to the piece of requesting code.