This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Tualha Khan
Ranch Hand
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Hemal Mehta
Ranch Hand
Posts: 101
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
Refer to www.absolute java's titbits in one of the titbits it is mentioned very clearly i don rember which 1
AMIT
 
Tualha Khan
Ranch Hand
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 198
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 242
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks santosh, got the point!!
Thanks once again!!
Bye,
Tualha Khan
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic