Reputation: 105
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?
Upvotes: 3
Views: 1272
Reputation: 10958
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.
Upvotes: 5
Reputation: 11
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
Upvotes: 0
Reputation: 267
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.
Upvotes: 0
Reputation: 32517
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
Upvotes: 2