aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes When to use keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "When to use keyword "const"? Just a reserved word. " Watch "When to use keyword "const"? Just a reserved word. " New topic
Author

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

Sam Sunamin
Ranch Hand

Joined: Mar 16, 2007
Posts: 113
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 ]

Yours Sam<br />SCJP5.0 97%<br />SCBCD5.0 72%
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
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.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Sam Sunamin
Ranch Hand

Joined: Mar 16, 2007
Posts: 113
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

Joined: Feb 28, 2007
Posts: 1707
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 ]

cmbhatt
Vidya Singh
Greenhorn

Joined: Mar 21, 2007
Posts: 28
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Sam Sunamin:
I found java has keyword "const". I found an example as following
...

Where did this "example" come from?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
The exam does not test you on the results of using Eclipse.


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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

Joined: Mar 16, 2007
Posts: 113
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When to use keyword "const"? Just a reserved word.