can anyone tell me what all method declaration are valid amongs t the follpw ing abstract int i; a) abstract int i; b] final int i; c] final volatile int i; d] public transient final i; e] protected static final int i; I think none is right b'coz when we declare final we need t o intialize the variable there only but why cant the variable be declared as abstract ???
You don't need to intialize a final variable at time of declaration. It is called a blank final, but it needs to be initialized before it is used. The keyword abstract pertains to methods and classes. It implies that the method or class is incomplete.
Hi Everyone, Please refer to the folowing code for my clarification on the querry posted:
Hope this clarifies, so (b),(c),(d) and (e) are correct answers. Ravindra Mohan. [This message has been edited by Ravindra Mohan (edited May 08, 2001).]
Ravindra, c is incorrect since volatile can't be used with the final keyword. Try to compile it yourself. d is incorrect since there is no reference type. Try to compile them yourself. Transient can't be used with the final keyword either. At least that's what my compiler tells me.
Originally posted by Sean Casey: Ravindra, c is incorrect since volatile can't be used with the final keyword. Try to compile it yourself. d is incorrect since there is no reference type. Try to compile them yourself. Transient can't be used with the final keyword either. At least that's what my compiler tells me.
Hi Sean, You are right - final and volatile won't go together. However the combination of transient and final is fine. The reason you got a compilation error is because the declaration shown in the question was wrong - try this: public transient final int i = 10; Take care, - Lam -
Hi Guys, Oops a correction from my earlier post, A VOLATILE variable cannot be declared as FINAL. I ran a code and the compiler told me the answer. I then looked for the same in the JLS which confirmed me the same. Please refer to JLS �8.3.1.4 volatile Fields at http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#36930 where this is clearly indicated :
A compile-time error occurs if a final variable is also declared volatile.