Skip to Content
Hardcore Java
book

Hardcore Java

by Robert Simmons
March 2004
Intermediate to advanced
344 pages
8h 50m
English
O'Reilly Media, Inc.
Content preview from Hardcore Java

Chapter 7. All About Constants

The function of constants is a concept that is often taken for granted by most developers. On the surface, they seem simple and trivial. However, constants that are not used correctly can be a major source of development headaches.

Although most developers regard constants as a single concept, there are actually three categories of constants: substitution, bit field, and option. Each of these categories has different dynamics and different issues to deal with.

Substitution Constants

Substitution constants are the simplest type of constants. Basically, they are substituted for something else in code. See Example 7-1.

Example 7-1. Substitution constants
package oreilly.hcj.constants;
public class SubstitutionConstants {
  /** Holds the logging instance. */
  private static final Logger LOGGER = 
                Logger.getLogger(SubstitutionConstants.class);

  /** A value for PI. */
  public static final double PI = 3.141;

  public double getCircleArea(final double radius) {
    double area = (Math.pow(radius, 2) * PI);
    LOGGER.debug("The calculated area is " + area);
    return area;
  }

  public float calculateInterest(final float rate, 
                                 final float principal) {
    final String LOG_MSG1 = 
                                "Error: The interest rate cannot be less than ";
                   final String LOG_MSG2 = ". The rate input = ";
                   final double MIN_INTEREST = 0.0;

    // -- 
    if (rate < MIN_INTEREST) {
      LOGGER.error(LOG_MSG1 + MIN_INTEREST + LOG_MSG2 + rate);
      throw new IllegalArgumentException("rate");
    }
    return principal * rate;
  }
}

This code declares ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Java Projects - Second Edition

Java Projects - Second Edition

Peter Verhas
Java 9 for Programmers

Java 9 for Programmers

Paul J. Deitel, Harvey Deitel

Publisher Resources

ISBN: 0596005687Errata Page