3
class Test {
    static final String name;

    {    
        name = "User"; 
        /* shows error. As i have to assign User as default value */
    }
    final String name1;

    {
       name1 = "User"; // This works but why the above one does not works
    }
}    

I'm able to assign values using static block but cannot by instance block why?

Prasanna
  • 95
  • 9

4 Answers4

5

Because it is static final, so it must be initialized once in the static context -- when the variable is declared or in the static initialization block.

static {    
    name = "User";
}

EDIT: Static members belong to the class, and non-static members belong to each instance of that class. If you were to initialize a static variable in the instance block, you would be initializing it every time you create a new instance of that class. It means that it would not be initialized before that, and that it could be initialized multiple times. Since it is static and final, it must be initialized once (for that class, not once for each instance), so your instance block will not do.

Maybe you want to research more about static vs. non-static variables in Java.

EDIT2: Here are examples that might help you understand.

class Test {
    private static final int a;
    private static int b;
    private final int c;
    private int c;

    // runs once the class is loaded
    static {
        a = 0;
        b = 0;
        c = 0;  // error: non-static variables c and d cannot be
        d = 0;  // referenced from a static context
    }

    // instance block, runs every time an instance is created
    {
        a = 0;  // error: static and final cannot be initialized here
        b = 0;
        c = 0;
        d = 0;
    }
}

All non-commented lines work. If we had

    // instance block
    {
        b++; // increment every time an instance is created
        // ...
    }

then b would work as a counter for the number of instances created, since it is static and incremented in the non-static instance block.

Community
  • 1
  • 1
ericbn
  • 10,163
  • 3
  • 47
  • 55
  • ya i agree with you but how this works final String name1; { name1 = "User"; } – Prasanna Mar 30 '15 at 19:26
  • @Prasanna, it works because your variable is non-static, it belongs to the instance. So `name1` will be initialized once by the instance block every time a new instance is created. – ericbn Mar 30 '15 at 19:43
2

Because static field has to be initialized upon class load. Instance block is invoked after class loading (upon new instance contruction) so final static fields cannot be changed. U can use static block to initialize final static fields

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
0

The static field is initialized when the class is loaded. That's why it won't work the way you're trying to make it work.

Skillet
  • 237
  • 1
  • 4
  • 16
0

SIB : It executes when the class gets loaded and executes only once in entire execution

IIB : It executes when the constructor is called but before the execution of constructor. So it executes as many times as constructor gets executed.

Let’s see the example

package com.kb.instanceblock;

public class SIB1 {

static {
    System.out.println("SIB");
}


{
    System.out.println("IIB");
}

public SIB1() {
    System.out.println("Constructor");
}

public static void main(String[] args) {

SIB1 sib = new SIB1(); }

} Output

SIB IIB Constructor

so when the class gets loaded, SIB is called and whenever constructor gets called, IIB executes and then constructor block executes.

Since as we know constant variables value should be defined at their declaration so we can not use iib for assigning static constant variables