Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

final and const

 
aakash bhatt
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between final and const keyword
Regards,
aakash
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The const keyword doesn't have any function in Java.
 
Jeff Bosch
Ranch Hand
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
Regards,
Jeff
 
Nischal Tanna
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
Nischal
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic