Intializing instance variables in java:

There are 3 ways to initialize instance variables of a class: 1. Initializing outside the constructor while declaring. 2. Initializing in the constructor. 3. Initializing in the initialization block
If your class is having any outside initializing then while creating the object it will call the outside initializing and then it will call the constructors.

1. Below is an example of initializing directly outside at the declaration.

If you are having pre-defined constant value to be assigned to your variable then do that outside.
         class Test
         {
           int i=10;
           int j;
           public Test()
           {
              j=20;
            }
          }

2. Below is an example of initializing outside and by using constructor.

1st way can be really useful if the initialization logic is simple. But if you want to put some loops or other complicated logic to initialize then we can't do it outside at the declaration part. We have to do it in the constructor. Especially if you want to initialize an array with some kind of loops, then do it in the constructor. And if we want to pass some dynamic values to initialize, then also we have to do it in constructor.
  class Test
     {
        int j;
        private final int max =100;
         public Test(int temp)
      {
        j=temp;
      }
      public Test()
      {
        j=200;
     }
}

3. Using initializing block:

Initialization block is a block with out any name with { open brace and } close brace with logic inside that. These are also called as unnamed blocks. This can be useful if you want to share a block of code between all the constructors, use it. Initialization block will be copied to all the constructors. This can be really useful to share a common piece of code with all the constructors.
                class Test
                {
                  int j;
                  int[] arr=new int[10];               
                  {
                  for(int i=0;i<10;i++)
                  {
	             arr[i]=i+1;
                   }
                }
                public Test()
                {
	           .....
                 }
               }
Below is one more example on initialization block.
      class Test
      {
	int j;
	int[] arr=new int[10];
	public Test(int temp)
	{
		j=temp;
	}
	public Test()
	{
		j=20;
	} 
	//below is the initialization block
	{
	    for (int i=0;i<10;i++)
	    {
	      arr[i]=i+1;
	     }
	   }
        }
When we use initialization block compiler will automatically copy that block into all the constructors. Note: In Java we should not write logic outside the functions. If we want to write we have to use blocks.

Initializing static variables in java:

1.We can directly initialize static variable outside the constructor.
              class Test
              {
	        int j;
	        private static int si=10;
                int[] arr=new int[10];
	        public Test(int temp)
	          {
		   j=temp;
	           }
                }
2.Initializing by using static initialization block (SIB)
      //this is the static initialization block
           static
           {
              int cnt=0;
               for(int i=0;i<10;i++)
               {
	         cnt=cnt+i;
               }
                si=cnt;
             }

Static blocks will be executed only once at the time of class loading. 1) Static block will not be copied into the constructors. 2) Initializing block will be executed all the time whenever you create the object, the reason is it will be copied to constructors.
      class Test
      {
	int j;
	private static int si;
	int[] arr=new int[10];
	public Test(int temp)
	{
		j=temp;
	}
	public Test()
	{
		j=20;
		si=100;//wrong
	}
       }   
Note: Do not initialize the static variable in the constructor, either initialize outside or in static block.