Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When to use keyword "const"? Just a reserved word.

 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found java has keyword "const". I found an example as following



How come final and const can be used together?

What's the different between final and const, and final static?


Thank you in advance.

The code is from some website and I cannot remember. Don't use it and it will cause compile fail.

Remember, const is just a reserved keyword, and don't use for methods, variables or anything else.


[ April 11, 2007: Message edited by: Sam Sunamin ]
[ April 12, 2007: Message edited by: Sam Sunamin ]
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Final is implemented as a variable cannot be changed, a class cannot be subclassed or a method can't be changed.

const is just a reserved word and doesn't do anything.
 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found the same answer in sun java forum. Remko, you are right. We don't need to care about "const". Thank you.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good reminder for the exam!

const does nothing, it is a keyword (but reserved not in use).
const can't be used as class/interface/enum/variable etc etc. name, hence it is reserved word.



Thanks all!
cmbhatt
[ April 11, 2007: Message edited by: Chandra Bhatt ]
 
Vidya Singh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I type the same program in Eclipse, it give syntax error on token const. So, is const supported by Java 5
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry!!!

I agree with Vidya. Using const gives compile time error "illegar modifier".
The top most post caused big confusion.
Please guide us to come to the correct conclusion regarding "const".


Thanks,
cmbhatt
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sam Sunamin:
I found java has keyword "const". I found an example as following
...

Where did this "example" come from?
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exam does not test you on the results of using Eclipse.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcus Green posted
The exam does not test you on the results of using Eclipse.


True, but in this case there is no difference in Eclipse's and the original compiler. Both indicate a compiler error at both occurences of "const".

Bu.
 
Sam Sunamin
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry to confuse your guys and I did not check the code in JDK. No, we did not use "const" in real java code.
[ April 12, 2007: Message edited by: Sam Sunamin ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic