OO Programming with Java


Primitives datatypes

More complex datatypes, such as dates or collections, are 'objects' too as illustrated below:

  1. What about day/month ? Use Date.
import java.util.Date;
public class Person {
  private Date birthdate;
  public Person(Date d)            { setBirthyear(d);  }
  public void setBirthDate(Date d) { birthdate = d;    }
  public Date getBirthDate()       { return birthdate; }
}

public class Demo {
  public static void main(String args[]) {
    Date date = new Date(2005,7,4);
    Person p  = new Person(date);
    System.out.println("Birthyear: "+ p.getBirthDate().getYear() );
  }
}

15 - 17