• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

constants

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Can anybody tell me what is a "named constant " and "literal constant".

I think a named constant is with modifier Final.
like : final String = "Good Morning".

and literal constant is just String = "Good Morning".


Please correct me if I am wrong.
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think

literal constant:
final int x = 2;

named constant:
static final int x = 2;
[ February 02, 2005: Message edited by: Marilyn de Queiroz ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's almost it. In both of Marilyn's examples, x would be considered a "named constant" because it is the name for a variable that holds a constant value. However, the value 2 assigned to x is a literal constant. A "literal constant" is basically a constant that is stated literally in the code. Here are some examples:

2
50000000L
3.1415
"Hello, World!"
'q'

A named constant is, as stated above, a variable that was declared final. Some examples from the standard API include

Math.PI
Calendar.JANUARY
BorderLayout.NORTH

I guess I should clarify that I am referring to these constants as <class>.<constant name>, but that isn't strictly necessary. Also note that whenever you declare a named constant, it must be given a literal constant as its value. Also, I don't think a named constant is required to be a static, although they often are.

HTH

Layne
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic