Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

J2SE 1.5: Java's Evolution Continues : Page 3

J2SE 1.5 (codename: Tiger) epitomizes Java's ongoing evolution and adaptation to the emerging needs of programmers and their applications. With features such as generics, autoboxing, and enhanced for loops, it makes Java code easier to develop and read.


advertisement
Typesafe Enums
Tiger brings to the table a new type named enum. Using this keyword, one can define a set of valid values or states. Consequently, you can ensure type safety during compile time. For example, if you wanted to realize the concept of enumerations in the past, you might have used constants like in the following code:

public class TrafficLight { public static final int GREEN = 1; public static final int RED = 0; public static final int YELLOW = 2; // light is green by default private int state = GREEN; // get the current light state public int getState() { return this.state; } // set the current light state // notice no protection of what the light can be set to public void setState(int state) { this.state = state; } }

The code of the EnumExample class demonstrates how you previously had to use a helper class to represent a set of states. Notice the absence of state setting safeguards. To elaborate, your states are represented by integers, and they have no safeguard to prevent the constructor of your TrafficLight from taking an integer greater than 2 (representing yellow). You could throw in some custom Boolean logic to add restrictions, but it would be quite cumbersome to create. Also, the numbers 0, 1, and 2 can lead to errors. You could accidentally assume that 0 represents green somewhere in your code. Now that would cause some traffic nightmares, wouldn't it?



Take a look at the method newMethodologyWithEnum(). When you use the enum, you create a new enumeration and clearly define what your possible states can be. Switching between states is easier and your code does not rely on an auxiliary class anymore:

import java.util.*; public class EnumExample { public static void main (String args[]) { oldMethodology(); newMethodologyWithEnum(); } public static void oldMethodology() { TrafficLight myLight = new TrafficLight(); // note that you could accidentally set your traffic // light state to 4, which has no meaning myLight.setState(TrafficLight.RED); int stateOfLight = myLight.getState(); switch(stateOfLight) { case TrafficLight.GREEN: System.out.println("Light is Green"); break; case TrafficLight.RED: System.out.println("Light is Red"); break; case TrafficLight.YELLOW: System.out.println("Light is Green"); break; } } public static void newMethodologyWithEnum() { enum ImprovedTrafficLight {green,yellow,red}; ImprovedTrafficLight improvedLight = ImprovedTrafficLight.green; // switching states is easier improvedLight = ImprovedTrafficLight.red; // notice the easier syntax and no need for a helper object switch(improvedLight) { case green: System.out.println("Light is Green"); break; case red: System.out.println("Light is Red"); break; case yellow: System.out.println("Light is Yellow"); break; } } }

Static Imports
Tiger's static import feature eliminates the need to prefix static members of a class with class names. It's similar to the package import facility you are used to (except that you import static members from a class rather than classes from a package). The following example class juxtaposes how you had to perform things in the past and how Tiger's offerings can make your code much more abbreviated:

// static import syntax import static java.util.Calendar.*; public class StaticImportExample { public static String getCurrentMonthWithoutStaticImport() { int i = java.util.Calendar.getInstance().get( java.util.Calendar.MONTH); switch (i) { case java.util.Calendar.JANUARY: return "January"; case java.util.Calendar.FEBRUARY: return "February"; case java.util.Calendar.MARCH: return "March"; case java.util.Calendar.APRIL: return "April"; case java.util.Calendar.MAY: return "May"; case java.util.Calendar.JUNE: return "June"; case java.util.Calendar.JULY: return "July"; case java.util.Calendar.AUGUST: return "August"; case java.util.Calendar.SEPTEMBER: return "September"; case java.util.Calendar.OCTOBER: return "October"; case java.util.Calendar.NOVEMBER: return "November"; case java.util.Calendar.DECEMBER: return "December"; } return null; } // example use of static imports // note the streamlined abbreviated syntax public static String getCurrentMonthWithStaticImport() { int i = java.util.Calendar.getInstance().get(MONTH); switch (i) { case JANUARY: return "January"; case FEBRUARY: return "February"; case MARCH: return "March"; case APRIL: return "April"; case MAY: return "May"; case JUNE: return "June"; case JULY: return "July"; case AUGUST: return "August"; case SEPTEMBER: return "September"; case OCTOBER: return "October"; case NOVEMBER: return "November"; case DECEMBER: return "December"; } return null; } public static void main (String args[]) { System.out.println(getCurrentMonthWithoutStaticImport()); System.out.println(getCurrentMonthWithStaticImport()); } }



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap