Author Topic:   Static variable.
RajeshParab
greenhorn
posted February 25, 2000 12:57 AM             
What are the proper ways to initialize the static variables SIZE and MIN_VALUE ?

01: class Base
02: {
03: static final int SIZE;
04: static float MIN_VALUE;
05:
06: Base() { }
07: void test() {
08: System.out.println("Base.test()");
09: }
10:
11:}

Select all valid answers.

a) Add the following lines to Base Class
static {
SIZE = 10;
MIN_VALUE = 10.3f;
}

b) Add the following lines to Base Class
{
SIZE = 10;
MIN_VALUE = 10.3f;
}

c) Add the following lines to Base Class constructor
SIZE = 10;
MIN_VALUE = 10.3f;

d) Modify lines 03 and 04 as
static final int SIZE = 10;
static float MIN_VALUE = 10.3f;

Answer is A & D
but i don't agree with A. What will happen to final modifier of variable SIZE. and is it possible to change final modifier in static varible??????????????????

maha anna
bartender
posted February 25, 2000 05:08 AM             
All static final vars MUST BE initialized when the class is LOADED itself. All static final vars are NOT given a default value. So if you donot assign a value when it is declared, you should atleast in one and only one of following static initializers. Now the compiler is happy that it knows the value of a static final var beforehand.

If I understatnd you correctly, you are asking about the absence of the final modifier in the static initializer.

code:

static {
SIZE = 10;
MIN_VALUE = 10.3f;
}


Here we are NOT changing the modifier at all. We are just referring the pre-declared static final var.What made you to think like that? Does this help for you?
regds
maha anna

[This message has been edited by maha anna (edited February 25, 2000).]

Durga Prasad Babu
unregistered
posted February 25, 2000 05:18 AM           
Mr.Rajesh,

There is one exception to final modifiers.

Final variables may be left blank at the time of declaration.
But, they must be assigned a value in the initializer(i.e in your case the static initializer).Another possibility is to initalize them in all the constructors.

But, in interfaces you must initialize the variables which are by default public static and final.

maha anna
bartender
posted February 25, 2000 05:39 AM             
one correction. ( I think it is important )
static final vars MUST be initialized either in declaration or in one of static initialization blocks.NOT in constructors.The compiler does not wait until the construction of the object.
But instance final vars may be initialized in the constructor. If it is initialized in constructor, ALL CONSTRUCTORS MUST initialize the instance final var.
In both cases once it is initialized we can not reassign a new value to them.

[This message has been edited by maha anna (edited February 25, 2000).]

Tony Alicea
sheriff
posted February 25, 2000 08:34 AM             
Durga: If the final variable is static it may not be assigned in any constructor. It has to be explicitly initialized when declared or in a static init blk.


...and that static initialization block must be after the variable declaration because the compiler can't make forward reference to the variable.

|