KC Advanced Java

0

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<br />@Table (name=&quot;emp&quot;)<br />public class Emp {<br />   ...<br />}<br />

or

@Webservice<br />public class MyWebService {<br />&nbsp;&nbsp;&nbsp; @WebMethod (operationName=&quot;echoString&quot;, action=&quot;urn:EchoString&quot;)<br />&nbsp;&nbsp;&nbsp; public String echo (String input) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return input;<br />&nbsp;&nbsp;&nbsp; }<br />}

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&lt;X, Y&gt; <br />{<br />&nbsp;&nbsp;&nbsp; private X first; <br />&nbsp;&nbsp;&nbsp; private Y second; <br />&nbsp;&nbsp;&nbsp; ...<br />}<br />

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

Share.

About Author

Aino Andriessen is a consultant on Enterprise Java, ADF, PL/SQL, XML, and SOA development and is Expertise Lead on Application Lifecycle Management (ALM). He has a strong interest in ADF, SOA, Maven, architecture, quality management, delivery and application lifecycle management. Aino publishes on the AMIS technology blog and has been a presenter at the ODTUG Kaleidoscope, Oracle Open World and UKOUG TechEbs.

Comments are closed.