Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Constant and Final  RSS feed

 
Jyothi Bhogadi
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
Can somebody please explain the difference between Constant variables and final variables...

Regards
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To declare a variable as constant in Java,
the word final is inserted before the normal variable declaration:
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

According to you..if we define a variable with final...

its a constant agreed...

then what abt

public static final ....

And this can be declared at class level...wheres final is avaiable

at both class and method level...

But apart from that you have a keyword "const" what is that used for..???

Regards
 
phil shea
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If "final" is in front of a variable then that variable value cannot be changed after initializing it.

If "final" is in front of a method, then that particular method cannot be overriden.

If "final" is in front of a class, then we cannot extend that particular class.

Correct me if i am wrong.

-phil
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We say that variables marked final are constant once initialized. Final has several uses which is why there is a distinction. You cannot override final method or extend final classes. It's a little misleading when it comes to an object reference. The object can be changed but the variable always refers to the same object.

Final references have several benefits but none of them are obvious and none are strictly necessary. Other languages have the notion of "final" calling it "constant" or "const" for short. In C++, const is much stronger, much more useful and much more meaningful.

In C++ this says you cannot change the reference to bubble once it's initialized, just like Java.

Bubble& const bubble;

Here we say that you can't change bubble itself, through the bubble reference, that's a LOT more useful:

const Bubble& bubble;

Here we say you can't change the reference or the bubble:

const Bubble& const bubble;

It's unfortunate Java does not support the notion of constant objects, but I'm not losing sleep over it.
[ August 26, 2005: Message edited by: Rick O'Shay ]
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But apart from that you have a keyword "const" what is that used for..???

The keyword 'const' is reserved in Java, but it is not used.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!