Java tutorial
/* * This file is under the licence Attribution-ShareAlike 3.0 Unported * (CC BY-SA 3.0) http://creativecommons.org/licenses/by-sa/3.0/ * * You are free: * to Share - to copy, distribute and transmit the work * to Remix - to adapt the work * * Under the following conditions: * * Attribution - You must attribute the work in the manner specified by the * author or licensor (but not in any way that suggests that they endorse you * or your use of the work). * * Share Alike - If you alter, transform, or build upon this work, you may * distribute the resulting work only under the same or similar license to * this one. * * With the understanding that: * * Waiver - Any of the above conditions can be waived if you get permission * from the copyright holder. * * Public Domain - Where the work or any of its elements is in the public * domain under applicable law, that status is in no way affected by the * license. * * Other Rights - In no way are any of the following rights affected by the * license: * Your fair dealing or fair use rights, or other applicable copyright * exceptions and limitations; * * The author's moral rights; * Rights other persons may have either in the work itself or in how the work * is used, such as publicity or privacy rights. * */ package com.qnoid.java.tRm1.dont; import java.io.UnsupportedEncodingException; import java.util.Calendar; import org.joda.time.DateTime; import org.joda.time.Years; /** * @author Markos Charatzas [http://forrst.me/Cue] * @date Jan 22, 2011 * * Don't abuse the constructor */ public class Person { private final String name; private final int age; public Person(String[] args) { this.name = args[0]; this.age = Integer.parseInt(args[1]); } public Person(byte[] name, Calendar dob) throws UnsupportedEncodingException { this.name = new String(name, "UTF-8"); this.age = Years.yearsBetween(new DateTime(Calendar.getInstance()), new DateTime(dob)).getYears(); } /* * How do you test that without getting in the constructor mess? */ public boolean isOlderThan(int age) { return this.howOldAreYou() > age; } public int howOldAreYou() { return this.age; } public String whatsYourName() { return this.name; } }