Scrigroup - Documente si articole

Username / Parola inexistente      

Home Documente Upload Resurse Alte limbi doc  

AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Grouping constants


+ Font mai mare | - Font mai mic


Trimite pe Messenger
Using Java operators
Building a Java program
Mathematical operators
Data Structures And Random Number Generation
Introduction to Swing: Buttons, Icons, Menus
Coding style
The Component Class
Web Applications
Your first Java program

Grouping constants

Because any fields you put into an interface are automatically static and final, the interface is a convenient tool for creating groups of constant values, much as you would with an enum in C or C++. For example:


// Using interfaces to create groups of constants

package c07;

public interface Months ///:~

Notice the Java style of using all uppercase letters (with underscores to separate multiple words in a single identifier) for static final primitives that have constant initializers Ė that is, for compile-time constants.

The fields in an interface are automatically public, so itís unnecessary to specify that.

Now you can use the constants from outside the package by importing c07.* or c07.Months just as you would with any other package, and referencing the values with expressions like Months.JANUARY. Of course, what you get is just an int so there isnít the extra type safety that C++ís enum has, but this (commonly-used) technique is certainly an improvement over hard-coding numbers into your programs. (This is often referred to as using ďmagic numbersĒ and it produces very difficult-to-maintain code.)

If you do want extra type safety, you can build a class like this:


// A more robust enumeration system

package c07;

public final class Month2

public String toString()

public final static Month2

JAN = new Month2('January'),

FEB = new Month2('February'),

MAR = new Month2('March'),

APR = new Month2('April'),

MAY = new Month2('May'),

JUN = new Month2('June'),

JUL = new Month2('July'),

AUG = new Month2('August'),

SEP = new Month2('September'),

OCT = new Month2('October'),

NOV = new Month2('November'),

DEC = new Month2('December');

public final static Month2[] month = ;

public static void main(String[] args)

} ///:~

The class is called Month2 since thereís already a Month in the standard Java library. Itís a final class with a private constructor so no one can inherit from it or make any instances of it. The only instances are the final static ones created in the class itself: JAN, FEB, MAR, etc. These objects are also used in the array month, which lets you choose months by number instead of by name. (Notice the extra JAN in the array to provide an offset by one, so that December is month 12.) In main( ) you can see the type safety: m is a Month2 object so it can be assigned only to a Month2. The previous example provided only int values, so an int variable intended to represent a month could actually be given any integer value, which wasnít too safe.

This approach also allows you to use == or equals( ) interchangeably, as shown at the end of main( ).

This approach was inspired by an e mail from Rich Hoffarth.

Politica de confidentialitate



Vizualizari: 495
Importanta: rank

Comenteaza documentul:

Te rugam sa te autentifici sau sa iti faci cont pentru a putea comenta

Creaza cont nou

Termeni si conditii de utilizare | Contact
© SCRIGROUP 2022 . All rights reserved

Distribuie URL

Adauga cod HTML in site