Constant « Class « Java Articles

Home
Java Articles
1.Build Deploy
2.Class
3.Core Library
4.Data Types
5.Database JDBC
6.Design
7.Development
8.File Input Output
9.Graphics Desktop
10.J2EE Enterprise
11.J2ME Wireless
12.JVM
13.Language
14.Library Product
15.Network
16.Security
17.SOA Web Services
18.Test
19.Web Development
20.XML
Java Articles » Class » Constant 
Using enumerated constants can make code more readable. For example, you might want to define a new data type named Color with constants RED, GREEN, and BLUE as its possible values. The idea is to have Color as an attribute of other objects you create, such as Car objects:

Note that for constant inlining to happen, several conditions must take place simultaneously. For example, it won't occur if the field initializer expression can be evaluated only at runtime:

This month's tool is a variation on a theme. I will be expanding on the idea of enumerated constants as covered in Eric Armstrong's July 1997 JavaWorld article, "Create enumerated constants in Java." I strongly recommend reading that article before you immerse yourself in this one, as I will assume that you are familiar with the concepts related to enumerated constants, and I will expand on some of the example code which Eric presented.

Lately, I've been doing a lot of XML parsing. Before I start writing a parser, I declare an interface that contains a constant for each tag name and attribute. Declaring each tag name allows me to change the markup language without altering my parsing code -- I can simply update the constants.

n "Constants" (JavaWorld, March 30, 2001), I wrote, "I'd be interested to hear how others declare their constants and why. I can post your responses in a future Q&A.;" Well, here are some reader responses.

w___w_w_.__j___av_a___2__s___._c___o_m | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.