Advanced Programming Effective Java Source Effective Java Jason

  • Slides: 7
Download presentation
Advanced Programming Effective Java Source: Effective Java, Jason Arnold Advanced Programming. All slides copyright:

Advanced Programming Effective Java Source: Effective Java, Jason Arnold Advanced Programming. All slides copyright: Chetan Arora

Coding Convention ● Function and Class Names ● Variable Names: Different convention for local,

Coding Convention ● Function and Class Names ● Variable Names: Different convention for local, class and static variables ● Length of each function ● Object Oriented Advanced Programming. All slides copyright: Chetan Arora

Static Factory public static Boolean value. Of(boolean b) { return (b ? Boolean. TRUE

Static Factory public static Boolean value. Of(boolean b) { return (b ? Boolean. TRUE : Boolean. FALSE); } ● Unlike constructors they have names: Better Readability ● Can return null ● Can return a subclass: compact API. ● Control number of instances floating around, Singletons ● Caching Advanced Programming. All slides copyright: Chetan Arora

Singletons ● Single Instance ● Private Constructor ● Static Factory Advanced Programming. All slides

Singletons ● Single Instance ● Private Constructor ● Static Factory Advanced Programming. All slides copyright: Chetan Arora

Util Class • Private Constructor

Util Class • Private Constructor

public class Person { private final Date birth. Date; public Person(Date birth. Date) {

public class Person { private final Date birth. Date; public Person(Date birth. Date) { this. birth. Date = birth. Date; } public boolean is. Baby. Boomer() { Calendar gmt. Cal = Calendar. get. Instance(Time. Zone. get. Time. Zone("GMT")); gmt. Cal. set(1946, Calendar. JANUARY, 1, 0, 0, 0); Date boom. Start = gmt. Cal. get. Time(); gmt. Cal. set(1965, Calendar. JANUARY, 1, 0, 0, 0); Date boom. End = gmt. Cal. get. Time(); return birth. Date. compare. To(boom. Start) >= 0 && birth. Date. compare. To(boom. End) < 0; } }

Static Blocks class Person { private final Date birth. Date; public Person(Date birth. Date)

Static Blocks class Person { private final Date birth. Date; public Person(Date birth. Date) { this. birth. Date = birth. Date; } //The starting and ending dates of the baby boom. private static final Date BOOM_START; private static final Date BOOM_END; static { Calendar gmt. Cal = Calendar. get. Instance(Time. Zone. get. Time. Zone("GMT")); gmt. Cal. set(1946, Calendar. JANUARY, 1, 0, 0, 0); BOOM_START = gmt. Cal. get. Time(); gmt. Cal. set(1965, Calendar. JANUARY, 1, 0, 0, 0); BOOM_END = gmt. Cal. get. Time(); } public boolean is. Baby. Boomer() { return birth. Date. compare. To(BOOM_START) >= 0 && birth. Date. compare. To(BOOM_END) < 0; } Advanced Programming. All slides copyright: Chetan Arora }