• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

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

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The exam does not test you on the results of using Eclipse.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Hey! You're stepping on my hand! Help me tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic