Scrigroup - Documente si articole

     

HomeDocumenteUploadResurseAlte limbi doc
AccessAdobe photoshopAlgoritmiAutocadBaze de dateCC sharp
CalculatoareCorel drawDot netExcelFox proFrontpageHardware
HtmlInternetJavaLinuxMatlabMs dosPascal
PhpPower pointRetele calculatoareSqlTutorialsWebdesignWindows
WordXml

AspAutocadCDot netExcelFox proHtmlJava
LinuxMathcadPhotoshopPhpSqlVisual studioWindowsXml

Grouping constants

java



+ Font mai mare | - Font mai mic



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:

//: Months.java



// 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:

//: Month2.java

// 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 Months.java 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 | Termeni si conditii de utilizare



DISTRIBUIE DOCUMENTUL

Comentarii


Vizualizari: 752
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 2024 . All rights reserved