Back to GT CS-1331

Accessers and Mutators:

When a variable is private, it can only be modified/accessed by a method inside of the class that it is stored in

Follow the proper format shown below when making an Accesser/Getter or Mutator/Setter.

Accessers (Getters):

  • Returns the value of a private variable (can’t return void)
  • Contains no parameters
  • Only needed for variables that are going to be read
  • Accessers/Getters for static methods must be static as well
private int numberVal = 0; 
//format (name method get'VarName')
public int getNumberVal(){
  return numberVal;
}

Mutators (Setters):

  • Used to modify a private variable
  • Has a return type of void (doesn’t return any value)
  • Contains parameter of same type as the variable that is being changed
  • Mutators/Setters for static methods must be static as well

Make sure to validate input before changing the value of a variable in a Mutator/Setter.

private int numberVal = 0;
//format (name method set'VarName')
public void setNumberVal(int newVal) {
  numberVal = newVal; 
}

Overloading Constructors:

You can create/overload multiple constructors in order to provide different ways of initializing an object

public class testC {
  private int a;
  private String b;
  private bool c;
  private static count;
    
  //First Constructor
  public testC(String bVal) {
    a = 0;
    b = bVal;
    c = false; 
    count ++;
  }
 
  //Overloading Constructor by creating another one
  public testC(int aVal, String bVal) {
    a = aVal;
    b = bVal;
    c = false;
    count ++;
  }
  
}

Constructor Chaining and this():

  • DRY Principle: Stands for Don’t Repeat Yourself. It is not best practice to rewrite redundant code multiple times
  • Constructor Chaining: A less specific constructor calling another more specific constructor in order to avoid rewriting redundant code
  • To constructor chain, use the ‘this()’ method
    • Must be the first statement in a constructor
    • You can chain multiple constructors together

Do not use 'new' to constructor chain. This will create two new objects instead of one

//Same code as last example, but with constructor chaining
public class testC {
  private int a;
  private String b;
  private bool c;
  private static count;
    
  public testC(String bVal) {
    this(0, bVal, false);
  }
 
  public testC(int aVal, String bVal) {
    a = aVal;
    b = bVal;
    c = false;
    count ++;
  }
  
}

Using ‘this’ as a reference:

  • ‘this’ can also be used a reference to avoid creating new variables simply for a private variable assignment
  • In a method’s body, any use of an identifier(variable name) that is also the name of a parameter represents the parameter instead of a variable
    • To represent a variable that has the same name as a parameter, add ‘this.’ before the identifier
//Method same as testC from last example but using 'this.' instead
public testC(int a, String b) {
  this.a = a;
  this.b = b;
  c = false;
  count ++;
}

The toString Method:

  • Every method has a built in toString() method that returns a string that represents an object
    • By default, it shows the class name and its memory address
    • We can override it by creating our own toString() method
  • Can be used to print values of multiple variables without calling an accesser/getter for each one.
  • If a non-string object is passed as input to println/print, Java will return the result from the toString() method.
//Same class as in the other examples above
public String toString() {
  return "The number is " + a + ", the text is " + b + ", and the t/f is " + c + ".";
}
 
//Calling toString in a different file
testC newTest = new testC("Hi");
 
//Both lines below will print the same value
System.out.println(newTest.toString());
System.out.println(newTest);