Home | Gaming | Programming | Play Online | Contact | Keyword Query
Games++ Games & Game Programming

Games++ Home
Games++ Gaming
Games++ Programming
Beta Testing Games
Free Online Games
Hints & Cheats

E-mail This Page
Add to Favorites


Web Games++

Cheat Codes
Trickster Wiki
Game Ratings
Gameboy Cheats
PlayStation Cheats
BlackBerry Games
Photoshop Tutorials
Illustrator Tutorials
ImageReady Tutorials



How To Use Globally Defined Constants

Java How To: Source Code

Via a class

This technique is useful for constants defined on a corporate level. They are very generic by nature. The CONSTANT class is included in the classpath.

public class CONSTANT
   public static final integer SUCCESS  = 1;
   public static final integer FAILURE  = -1;
   public static final integer NOTFOUND = 0;

Since the members of the class are defined as "static", there is no need to instantiate the class. To use a constant, simply use CONSTANT.[constant name]

if (myMethod()==CONSTANT.SUCCESS) 

Via an interface

This technique can be used if the constants are not really global but especially designed to be used in a specific application for example. An application-level class needs to implement the interface to be able to see the constant definitions.

public interface APPCONSTANT 
   public static final String APPNAME    = "The Super APP";
   public static final String APPVERSION = "version 1.0";
   public static final String DBDRIVER   = "oracle.jdbc.driver.OracleDriver";

To use a constant, simply implement the interface

public class TheAppFrame extends Frame implements APPCONSTANT 

Copyright © 1998-2007, Games++ All rights reserved. | Privacy Policy