The road less taken

« Previous page | Main | Next page »
Tuesday Mar 04, 2008

Final keyword in Java

The Final keyword is used in Java on an entity that cannot be changed.

Here are a few different contexts in which final can be used:

Final class:

Meaning: A final class cannot be subclassed. Advantage: Security or efficiency. Example: java.util.System

Final method:

Meaning: The method cannot be overridden in a subclass. Advantage: Preventing unexpected behavior crucial to the functionality of the class.

Final variable:

Meaning: The variable is immutable, meaning once assigned, it cannot be reassigned. This is different from constant keyword since constant should be known at compile time and final need not be. Advantage: Optimization.

Friday Feb 29, 2008

This leap year: hop to it!

Today is what makes this year a leap year!

Tuesday Feb 26, 2008

JPA Query with wildcards

Here is a short tip on using LIKE expression with JPA .

What we are trying to do is get all the items that matches a pattern anywhere in their name.

In simple SQL, what I want to do is:

SELECT userName FROM Profile p WHERE p.userName LIKE %pattern%;

I'm using annotations to create a named query.

@NamedQuery(name="Profile.getUsernameWithPattern", 
query="SELECT p FROM Profile p WHERE p.userName LIKE ?1 ORDER BY p.userName ASC")

(This will even sort the result!!)

Here's the code that demonstrates using this query:

 Query query = strategy.getNamedQuery("Profile.getUsernameWithPattern");
 query.setParameter(1, "%" + pattern + "%");        
 query.getResultList();

Hope you found this tip helpful!

Monday Feb 25, 2008

Localizing field labels in struts 2

A short tip about localizing field labels in your jsp page in struts 2. First off, the message should be present in the message resource bundle. Now how do you read it into the field label?

Here's how:

<s:password label="%{getText('password')}" name="password" />

How does this work?

The framework takes care of this.
In the text filed, the expression %{getText('password')} tells the framework to lookup "password" in the message resources.

Wednesday Feb 20, 2008

On code monkeys and project schedules

On project scheduling ...

and deliverables ...

and implementation ...