Showing posts with label Learn java 5. Show all posts
Showing posts with label Learn java 5. Show all posts

Wednesday, June 1, 2011

Aspects in Java

Aspects in Java
Lets take an example of a Student class. Student class is responsible for calculating marks of a Student.


class Student{
calculateTotalMarks(){
....
....
}
}
Now, after six months, your manager tells you to add some code, which will be called before calculateTotalMarks () method will be called.

One solution is to add that code above the first line of method statement. For e.g
class Student{
calculateTotalMarks (){
// Do what manager told me to do
....
….
}
}

But, it might happen that this method is being called from some other places in your project. So, changing calculateTotalMarks() may break the code.
This is where aspects come into picture. You don’t need to modify Student class. Simply create an aspect class and define pointcuts. For example:

public aspect StudentAspect {
pointcut monitorStudent()
: execution(* Student.calculateTotalMarks (..));

before() : monitorStudent (){
getStudentDetails();
}

after():monitorStudent (){
calculateAverageMarks();
}
}

Here, I have defined a StudentAspect aspect class and before/after pointcuts. When ever calculateTotalMarks method of Student will be called, getStudentDetails will be called before that and after execution of calculateTotalMarks method, calculateAverageMarks method will be called.

-getStudentDetails method will be called
-calculateTotalMarks method will be called
-calculateAverageMarks method will be called

Syntax
pointcut monitorStudent()
: execution(* Student.calculateTotalMarks (..));

Pointcut monitorStudent() – declare pointcut with name monitorStudent

execution(* Student.calculateTotalMarks (..)) – Apply pointcuts to any method which resides in any package with Student as class name and calculateTotalMarks as method name with any number of arguments.

Saturday, July 18, 2009

Generics in Java 5

Arrays in Java have always been type safe. For example, an array declared as type integer can only accept integers (not String, Dog, Cat). But, collections are not like that. There is no syntax for declaring type safe collections in pre java 5. To create an ArrayList of integers, you would say:

ArrayList lst = new ArrayList();

Similarly, for creating ArrayList of String, you would say:

ArrayList lst = new ArrayList();

There is no difference between the above two declarations. They are declared of type integer and String respectively, but you can put anything into it. Like, Dog object, Cat Object as so on.

As of java 5, you can use type safe collections. If you want to create an ArrayList of type String, you would say,


ArrayList lst = new ArrayList();
OR
List lst = new ArrayList();



How you add elements to collection in pre java 5?

List lst = new ArrayList ();
lst.add (“Danny”);
lst.add (new Cat ());
lst.add (new Double ());

As you can see, a non- generic collection can hold any kind of Object.


How you add elements to collection in java 5?

List lst = new ArrayList ();
lst.add (“Danny”);
lst.add (“Katty”);
lst.add (new Cat ()); // Compilation Error


You can’t add Cat object in lst ArrayList – You can add only and only Strings.


How to get objects from collection in Pre java 5?

In pre-java 5, the method that get objects out from collection could have only one kind of return type – java.lang.Object. If you want to get element from String list, you require a cast to do so.

String element = (String) lst.get (0);

and since, you can’t guarantee that what is coming out really is a String, the cast could fail at run time.

How to get objects from collection in java 5?

By using generic syntax, you can ensure that what is coming out is what you have added. So, no cast is required here.

String element = lst.get (0);

You are sure that lst List holds only Strings, so no cast is required here.

Thursday, July 16, 2009

Enums in Java 5

Following are the advantages of using Enums:

  • Enums are type safe.
  • Enums are Serializable and Comparable by default
  • Programmers doesn’t require to do extra work of implementing toString(), equals() and hashCode(). Enums provide implementation of these by default.
  • Programmers can use Enums in switch-case statements.
  • Enums are permitted to implement to interfaces.

Java 5.0 lets you to confine a variable to have only a fixed set of predefined values. For example, a cold drink shop may want to serve cold drinks in just three sizes- SMALL, MEDIUM, and LARGE. With this simple declaration, it can be guaranteed that the compiler will stop you from assigning anything to cold drinks except SMALL, MEDIUM, and LARGE.

Syntax enum DrinkSize {SMALL, MEDIUM, LARGE};

Now, if you want to get DrinkSize, you will do something like this

DrinkSize ds = DrinkSize.SMALL;

According to sun coding convention, all constants should be in caps, although you can give these in small letters also.

One important rule you need to remember about Enums is:

They must not be declared within a method.

How to declare Enum outside a class?

enum DrinkSize { SMALL, MEDIUM, LARGE } // this cannot be

// private or protected

class Drink {
DrinkSize size;
}
public class DrinkTest {
public static void main(String[] args) {
Drink drink = new Drink ();
drink.size = DrinkSize.BIG; // enum outside class
}
}

Key point to remember here is that the enum can be declared with only public or default modifier.

How to declare Enum inside a class?

class Drink {
enum DrinkSize { SMALL, MEDIUM, LARGE }
DrinkSize size;
}
public class DrinkTest {
public static void main(String[] args) {
Drink drink = new Drink();
drink.size = Drink. DrinkSize.SMALL; // enclosing class
// name required
}
}

At the end of the enum, putting a semicolon is optional

enum DrinkSize { SMALL, MEDIUM, LARGE };