Home arrow Java & J2EE arrow Page 3 - Java Comes of Age

Static Import and Generics - Java

Time to open the Champagne -- Java 1.5 is out, and the language has finally come of age! With the new Java 1.5 specification, Java now contains features that make it feel like a proper "grown-up" language. The rest of this article will introduce you to these new features. To try out the features for yourself, simply download Java 1.5 from Sun’s website and give it a whirl. Note that you’ll need to compile the code using the –source 1.5 option; otherwise, you’ll get compilation errors when using the new features.

TABLE OF CONTENTS:
  1. Java Comes of Age
  2. Type-Safe Enumerations
  3. Static Import and Generics
  4. Enhanced For Loop
  5. Auto-boxing, Auto-Unboxing, and VarArgs
  6. Meta-data
By: Simon White
Rating: starstarstarstarstar / 20
April 07, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Static Import

If you take exception to such lengthy references, in Java1.5 you can now use a static import:


import static card.Card.CardValue.*;
import 
static card.CardSuit.*;

This makes all the values defined for CardSuit and CardValue available to the importing class as if they were defined in the same class. For example:


Card sixHearts = new Card(sixhearts);

Static imports can be used to import static identifiers and static methods as well as enumerations.

Generics

Something that I always found irksome about Java was the difference in the strength of typing between arrays and collections. If an array of objects is used to hold some data, then every object in the array has to be a (direct or indirect) instance of the same class. Any attempt to assign an object of some other class to an element of the array results in a compile-time error. On the other hand if a collection is used in Java 1.4 or earlier, then it is a (weakly-typed) collection of objects. This means that assigning an object of some other class to an element of the collection is perfectly acceptable to the compiler, even though it may not have been the intention of the programmer. Such situations give rise to a run-time error (typically a ClassCastException). In other words, the use of collections placed an additional burden on the programmer to avoid run-time errors that could not occur when arrays were used. The programmer was faced with a trade-off between the flexibility of collections and the type-safety of arrays. The good news is that this trade-off no longer exists! From Java 1.5, collections can be strongly typed.

Instead of saying:


List deck = new ArrayList();

and (at most) commenting the intention that the List is to contain only objects of the class Card, we can now enforce that constraint programmatically:


List<Carddeck = new ArrayList<Card>();

There is much more to be said about generics than can be covered in this article, but the basic idea should be clear. For more information, take a look at Sun's Generics Tutorial.



 
 
>>> More Java & J2EE Articles          >>> More By Simon White
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: