KC Advanced Java html

KC Advanced Java

The last KC Web and Java session was titled ‘Advanced java’. During that session we got presentations and workshops on four subjects: annotations, threads, introspection/reflection and generics.

Annotations, also called java metadata, provide a way to declarative add extra information to the java code, without the need for external (XML) files and supported with compile time verification. Annotations are available either at source level, class load time or at runtime, declared by the retention policy and can be defined for almost all code constructs like a class, a method, a constructor, a field etc. The annotation is used before the target in the form @AnnotationType and can also contain attributes. For example:

@Entity
@Table (name="emp")
public class Emp {
   ...
}

or

@Webservice
public class MyWebService {
    @WebMethod (operationName="echoString", action="urn:EchoString")
    public String echo (String input) {
        return input;
    }
}

The java language (v5) contains only a few annotations, but the next JEE version will use many annotations, many tools already use annotations and if that is not enough, you can also write your own. The presentation was very informative and the workshop provided an excellent way to try it out.

Threads are always considered a difficult subject. And although it may be quite a challenge to run and organize multiple processes within your application. But during the workshop we experienced that the basics are actually quite simple. Java 5 has added enhancements and new functions (java.util.concurrent) to the thread functionality.

Reflection/introspection offers fucntionality to analyze Java code at runtime. It offers a way to see how the code looks like. It provides access to classes, methods, variables etc., even to private members. You can even invoke a method using reflection, although it comes with a prize: performance may drop even thousendfold. And although it is fun to use, it’s uses are probably limited to tool, ide and framework development.

Generics is probably the most important feature of Java 5. It offers a way to parameterize your code and add type-safety to your code where could’t before. It is most notable, but centainly not limited, for collections. You can now, for example,  create a List that only contains certain objects like Strings: List<String>. You can also use it in classes and for properties, for example to handle more more types of objects but without loosing type-safety:

public class Pair<X, Y> 
{
    private X first; 
    private Y second; 
    ...
}

Take a look at the Java5 JavaDoc to see how extensive it has been added to the Java language.

Although the time was too limited to gain real experience, it provided a good overview of some (little bit) more advanced Java features. Especially generics, but also annotations, must be on the top list of every Java developer for they (will) play a major role in all Java development. Although threads and reflection offer a window into the inner workings of Java, their use is probably limited to specific situations.

 

Resources

Presentations and workshop files

Sun Reflection Tutorial