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 b) Add the following lines to Base Class c) Add the following lines to Base Class constructor d) Modify lines 03 and 04 as Answer is A & D
|
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: 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, 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.
|
| | |