13.07.2015 Views

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

Java™ Application Development on Linux - Dator

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

90Chapter 3An Experienced Programmer’s Introducti<strong>on</strong> to JavaTIPA comm<strong>on</strong> technique used with public static final c<strong>on</strong>stants is to putthem in an interface definiti<strong>on</strong>. (This is the excepti<strong>on</strong> to the rule that interfacesdefine method signatures but c<strong>on</strong>tain no data.) When a class wants to use <strong>on</strong>eor more of those c<strong>on</strong>stants, it is declared to implement that interface:public MyClassextends BigClassimplements Comparable, LotsaC<strong>on</strong>stants{...}In defining MyClass we have declared that it implements LotsaC<strong>on</strong>stants(not a name that we recommend you using). That makes all the c<strong>on</strong>stants thatwe have defined inside the LotsaC<strong>on</strong>stants interface available to theMyClass class. Since classes can implement many different interfaces, thisdoesn’t interfere with the use of other “real” interfaces, such as Comparable.WARNINGThe keyword enum is new to Java 5.0, so older programs that may have usedenum as a variable name and will now cause an error when recompiled forJava 5.0.The enum will look very familiar to C/C++ programmers, but there aresome important differences. In C/C++ the values of the enum elements are, inreality, integer values. Not so in Java. Here they are their own type, but can bec<strong>on</strong>verted to a String via the toString() method, with a value that matchesthe name, for easy reading and debugging.Enumerated values can be used in == comparis<strong>on</strong>s since they will bedefined <strong>on</strong>ly <strong>on</strong>ce (like other static final c<strong>on</strong>stants) and it would <strong>on</strong>ly bereferences that are passed around. They would be referenced by the name ofthe enumerati<strong>on</strong> followed by dot followed by the particular value (e.g.,WallMods.WINDOW) and used as an object. (We have used uppercase for thenames not out of any syntax requirement, but <strong>on</strong>ly to follow the typical namingc<strong>on</strong>venti<strong>on</strong> for c<strong>on</strong>stants.)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!