OCA Java SE 8 Method - Java Class Design








JavaBeans naming conventions

Properties are private.

private int numEggs; 

Getter methods begin with is if the property is a boolean.

public boolean isHappy() {  
   return happy;  
} 

Getter methods begin with get if the property is not a boolean.

public int getNumEggs() {  
   return numEggs;  
} 

Setter methods begin with set.

public void setHappy(boolean happy) {  
   this.happy = happy;  
} 

The method name must have a prefix of set/get/is, followed by the first letter of the property in uppercase, followed by the rest of the property name.

public void setNumEggs(int num) {  numEggs = num; } 

Suppose we have two private instance variables.

private boolean playing; 
private String name; 

The following line doesn't follow the JavaBeans naming conventions. Since playing is a boolean, the getter must begin with is.

public boolean getPlaying() { return playing; } 

The following line is a correct getter for playing.

public boolean isPlaying() { return playing; } 

Line 16 doesn't follow the JavaBeans naming conventions because it should be called getName.

public String name() { return name; } 

The following two lines do not follow the JavaBeans naming conventions because they should be named setName.

public void updateName(String n) { name = n; } 
public void setname(String n) { name = n; } 




Creating Immutable Classes

Immutable classes are useful because they will always be the same.

Immutable is only measured after the object is constructed. Immutable classes are allowed to have values. They just can't change after instantiation.

You can pass immutable classes around your application with a guarantee that the caller didn't change anything.

public class Bird { 
  private int numberEggs; 
  public Bird(int numberEggs) { 
    this.numberEggs = numberEggs; 
  } 
  public int getNumberEggs() { 
    return numberEggs; 
  } 
} 




Return Types in Immutable Classes

When you are writing an immutable class, be careful about the return types.

The following code isn't really immutable class. Consider this code snippet:

class NotImmutable { 
   private StringBuilder builder; 
   public NotImmutable(StringBuilder b) { 
     builder = b; 
   } 
   public StringBuilder getBuilder() { 
     return builder; 
   } 
} 

public class Main{
   public static void main(String[] argv){
        StringBuilder sb = new StringBuilder("aaa"); 
        NotImmutable problem = new NotImmutable(sb); 
        sb.append(" CHANGED"); 
        StringBuilder gotBuilder = problem.getBuilder(); 
        gotBuilder.append(" changed again"); 
        System.out.println(problem.getBuilder()); 
   }
}

The problem is that we are just passing the same StringBuilder all over. The caller has a reference since it was passed to the constructor.

A solution is to make a copy of the mutable object. This is called a defensive copy.

public Mutable(StringBuilder b) { 
  builder = new StringBuilder(b); 
} 
public StringBuilder getBuilder() { 
  return new StringBuilder(builder); 
} 

Another approach for the getter is to return an immutable object:

public String getValue() { 
  return builder.toString(); 
}