Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final modifier - How!!!????!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "final modifier - How!!!????!!!!" Watch "final modifier - How!!!????!!!!" New topic
Author

final modifier - How!!!????!!!!

Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
Hey,
Khalid Moughul's book says that " a final variable need not be initialized at it's declaration, but it must be initialized once before it is used. These variables are also known as blank final variables".
Going according to the above quote, my following program should perfectly compile and also print "Done It" on the screen, since I am just declaraing the the final variables, but not using them anywhere.
************************************
class test155
{
final int i;
final static int b;
public static void main(String args[])
{
System.out.println("Done It");
}
}
Compiling the above code, gives the following error on my system:
test155.java:3: Blank final variable 'i' may not have been initialized. It must be assigned a value in an initializer, or in every constructor.
final int i;
^
test155.java:4: Blank final variable 'b' may not have been initialized. It must be assigned a value in an initializer, or in every constructor.
final static int b;
^

*************************************
Also, Can somebody clearly pin point the differnce between an interface and an abstract class having abstract methods?
Bye,
Tualha Khan

SCJP2, BEA WLS 6.0, DB2 UDB 7.1
Hemal Mehta
Ranch Hand

Joined: Nov 16, 2000
Posts: 101
You need to initialize it either in main or in a constructir before using it. The intialization rferred to in Khalids book refrets to this but ur code doesn't have any intilazation.
Amit Roy
Ranch Hand

Joined: Oct 10, 2000
Posts: 132
hi
Refer to www.absolute java's titbits in one of the titbits it is mentioned very clearly i don rember which 1
AMIT


<I>Chance Favours the Prepared minds"</I>
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
Dear Hemal,
I know that i should initialize the final variables before using them. This can be done either in main() or in the constructor or otherwise. But my point is that, if I am not using them in my program then I don't need to (or want to) initialize them.
Khalid's book says, that I need not initialize them while declaring them, but I should initialize them BEFORE I start to USE them.
Now in my program, I am just initialing them, but not using them. So it should not give me any problem.
Please clarify this point to me, maybe I am wrong or confused, I need a little helping hand here.
Thanks.
Bye,
Tualha Khan
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
sorry, I mean I am just declaring them and not initializing them. See in the second or third paragraph of the message.
Bye,
Tualha Khan
Bharatesh H Kakamari
Ranch Hand

Joined: Nov 09, 2000
Posts: 198
i) Interfaces are the means of multiple inheritance in java where as you cannot extend more than one abstract (or concrete) class. You can implement more than one interface.
ii) Interfaces cannot contain implementation of the methods whereas abstract class can contain concrete methods.
iii) In interfaces variables are public,static and final implicitly.
HTH
Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
Hi Tualha Khan,
Java compailer gives proper error messages when final variable needs to be initialized. It says "Blank final variable 'i' may not have been initialized. It must be assigned a value in an initializer, or in every constructor." If you have two constructors and if you are initializing in one constructor, you still get the error message. Pl note "It must be assigned a value in an initializer, or in every constructor".
Hope this is clear for you.
Santhosh Kumar.
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
thanks santosh, got the point!!
Thanks once again!!
Bye,
Tualha Khan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final modifier - How!!!????!!!!
 
Similar Threads
CDC porting for Linux-PowePC
Damm Final Variable
STRANGE??????Final variables
final instance variables
final variable