This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes final and const Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "final and const" Watch "final and const" New topic

final and const

aakash bhatt
Ranch Hand

Joined: Jan 09, 2003
Posts: 182
What is the difference between final and const keyword
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
The const keyword doesn't have any function in Java.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
"const" is a C++ keyword, the equivalent of "final". It's reserved in Java, presumably in case they ever decide to implement it. If they do, they will probably implement it in method parameters to let the compiler know that the values passed in cannot be modified.
Currently, final works for parameters, but const would probably have one difference: When using final on an object reference, the reference itself is final, not the object. By using const, the object could be marked unchangeable, at least by the method that sees the const reference.

Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Nischal Tanna
Ranch Hand

Joined: Aug 19, 2003
Posts: 182
final keyword in Java can be used with:
methods,variables and classes
1.if used with a method .the method cannot be overridden by a child class.
2.if used with a variable,the variable value remains constant
3.if used with a class,the class cannot be inherited i.e., no child of a final class..
hope this works..

It is sorta covered in the JavaRanch Style Guide.
subject: final and const
jQuery in Action, 3rd edition