Browsing articles in "Technology"

Advanced Java – Part 1 – Beans

Feb 20, 2011   //   by Martijn   //   Technology  //  Comments Off

The first part of this series is about Beans. Even though this subject is neither difficult nor advanced, it will come up quite a few times in this series and be used in examples. Therefore it is briefly discussed here.

A bean is a Plain Old Java Object (POJO). In order to be a bean the POJO must obey certain conventions about its structure. These conventions state that a bean must:

  1. Have a default constructor with no arguments. So that it can be automatically instantiated (using Reflection)
  2. Provide access to its variables using getters and setters. For example, the variable “name” should have a method “getName()” and a method “setName(…)” to get and set its value. Boolean getters start with “is” instead of “get”.
  3. Be Serializable. So it’s state can be stored and recalled (using Serialization)

The following class is an example of a bean. It is a very basic representation of a Student.

package advanced.java.bean;
 
import java.util.ArrayList;
import java.util.List;
 
public class Student {
    private String name;
    private int age;
    private boolean awake;
    private List<String> courses;
 
    public Student() {
        setName("");
        setAge(0);
        setAwake(false);
        setCourses(new ArrayList<String>());
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public int getAge() {
        return age;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public boolean isAwake() {
        return awake;
    }
 
    public void setAwake(boolean awake) {
        this.awake = awake;
    }
 
    public List<String> getCourses() {
        return courses;
    }
 
    public void setCourses(List<String> courses) {
        this.courses = courses;
    }
 
}

An object of class Student can be used to store the information of a real student.

Student student = new Student();
 
student.setName("Fred");
student.setAge(19);
student.setAwake(true);
student.getCourses().add("English");
 
if (student.isAwake()) {
    student.getCourses().add("Maths");
}

Overview | Serialization >>

Advanced Java – Overview

Feb 18, 2011   //   by Martijn   //   Technology  //  Comments Off

This series of blog posts is meant to give students and recent graduates an idea of what is out there beyond what they have been taught. There is plenty of information about these subjects on the Internet, so these articles will not go into too much detail. They are meant as a collection of cool Java topics.

The Blogposts

The first few posts will cover the following topics:
1. Beans
2. Serialization
3. Reflection
4. Annotations
5. Proxies
6. Persistence (JPA & JDO)
7. jUnit
8. Threading
9. Servlets & JSP’s
10. Vaadin

In these Advanced Java posts there will most likely be references to various development tools that are common in modern Software Development. I will cover some of these in a series called Development Tools. The first few posts will cover:
1. Maven
2. SVN
3. Eclipse & Plug-ins
4. Tomcat & WTP
5. Hudson & Nexus
6. Google App Engine
7. Google Web Toolkit

The Running Example

The examples that will be given throughout this series will fit the following situation:

You have accepted a summer job at a university. Your task is to write a java library for storing and printing lists of students. You are given a class Student.java that is already being used in three other applications. Your library has to have a class that implements the following interface:

package advanced.java;
 
public interface StudentManager {
    public List<Student> getStudents ();
 
    public void addStudent (Student student);
 
    public void removeStudent (Student student);
 
    public void save (String filename);
 
    public void print ();
 
    public void print (PrintStream out);
}

There is one more requirement: the library will be used by professors at the university in their applications. They will undoubtedly extend Student to add their own necessary information. You have to make sure your library also stores and prints this additional information, even though most of these sub-classes haven’t even been written yet.

Let’s move on to the first post:
Beans >>