toString Method • The toString method is a method that takes no parameter and returns a string • A returned string usually contains information on instance variables of its class. • Each class has a default toString method that contains its class object name and hash number. • When an object is used with System.out.println method, its toString method will be called.
and Setters public class Example { private int number; //Getter or Accessor method public int getNumber() { return number; } //Setter or Mutator method public void setNumber(int numberFromOutside){ number = numberFromOutside; } }
and Setters public class Example { private int number; //Getter or Accessor method public int getNumber() { return number; } //Setter or Mutator method public void setNumber(int numberFromOutside){ number = numberFromOutside; } }
• The null reference – the reference variable that does not currently point to an object. • Any variable of a class that was not instantiated/created using its constructor is a null. • If a program tries to access methods or variables, then you will get NullPointerException error during the execution of the program. In that case, check your program to see if it called a constructor to instantiate it.
• The this reference refers to its class itself and can be used to refer to the instance variables of an object of the class. • You can create an instance variable and a local variable using a same name (compiles), but it is confusing! • In such case, local variables will be used, and instance variables will be ignored. We can use the this reference to refer to instance variables to distinguish them.