Unit 2 introduces the concept of an object, including calling methods of an object. This includes the methods of String
objects, as well as the Math
class methods.
Dog
class can define instance variables related to the dog's characteristics (like its name, breed, and color). Then, multiple instances of the Dog
class can be created, each with different names, breeds, and colors.Constructors are special methods used to instantiate classes
Parts of a constructor
Dog(String name, String breed, String color)
Dog
. The same as the class name.String name, String breed, String color
. Specifies the values that can be passed into the constructor, which are parameters. Parameters are used by the newly-created object to set its initial state, including its instance variables.
name
parameter would be used to set an instance variable for the Dog
's name.Calling a constructor
new
keyword and call the class' constructor with the appropriate parameters.new Dog("Fido", "Golden Retriever", "brown");
Dog myDog = new Dog("Fido", "Golden Retriever", "brown");
Actual vs. formal parameters
Dog
example, String name, String breed, String color
is the formal parameter list, and "Fido"
, "Golden Retriever"
, "brown"
are the actual parameters.Overloaded constructors and parameter compatability
// Overloaded constructor signatures
Dog(String name)
Dog(String name, String breed, String color)
// Illegal because Java doesn't know which to use
Dog(String name, String breed)
Dog(String name, String color)
// Constructors
Dog(String name)
Dog(String name, String breed, String color)
Dog(int age, String name)
new Dog("Fido"); // ok
new Dog("Fido", "Golden Retriever", "brown"); // ok
new Dog("Fido", "Golden Retriever"); // not ok
// no matching constructor
new Dog(10, "Fido"); // ok
"Call by value"
Constructors can be used to instantiate any class, including classes written by others.
String
class can be instantiated with its constructor.Objects have methods, which are groups of code that are used to perform a specific task. The set of methods available to an object is called its behavior.
Method signatures are similar to constructor signatures. They include the method's name and the method's parameter list. However, method signatures also have a return type, which specifies the type of the value that the method returns.
void
return type means that the object returns no value.// Example method signatures for Dog
void birthday()
void bark()
String getName() // return type of String
int getAge() // return type of int
void setName(String name)
null
In Java, null
is a special keyword that represents an empty object reference, i.e. an object reference that does not point to any actual object.
A variable declared with its type as the name of a class is said to have a reference type.
Dog myDog; // myDog's type is a reference type
// as opposed to a primitive type
A variable of a reference type has two possibilities as to what it can hold:
null
value, indicating that the variable does not currently point towards any object.Trying to call any method or access any instance variable of a variable storing a null
value will result in a NullPointerException
at runtime, since the null
value is not an object and has no methods or instance data.
Dog aDog = null; // this dog hasn't been created yet
aDog.getName(); // throws a NullPointerException since
// there is no dog to get a name for