Creating Objects and Calling Methods
Classes
Consider a blueprint for a house. A builder can construct that same style house at various different addresses. The houses, aka objects, each have a distinct address, they might have a different color, different roof material, maybe a different number of bedrooms or bathrooms. But they are all built on the same blueprint and in the same style. Classes are similar to blueprints. They tell the computer how to create an specific object or instance of the class.
Classes are comprised of attributes and behaviors.
- attributes are the data values that define the class or objects of the class. They are defined through instance variables.
- behaviors are what the class or objects of the class can do. They are defined through methods.
Declaring and Initializing an Object
A specific instance of a class is called an object. We can declare and initialize many objects or instances of a class.
A constructor is used to create an object and initialize the values, or instance variables, for the object. A constructor always has the same name as the class. A constructor has a parameter list in parenthesis (this list can be empty).
A parameter list, is similar to a record component list, and allows arguments to provide additional data the constructor needs to initialize instance variables.
When creating an object using a constructor, the Java keyword new
is used. The following pattern is used:
ClassName objectName = new ClassName(argument1, argument2, ...);
The values in parenthesis are called arguments. When we create an object or instance of a class, each parameter in the parameter list will need a corresponding argument.
Example of Dice Class
Consider a Dice
class. The Dice
class has two constructors:
- a default constructor that doesn't take any parameters. It will set the number of sides of the dice to
6
.
- a constructor that has one
int
parameter. It will set the number of sides to a provided int
value.
Dice sixSided = new Dice();
Dice twelveSided = new Dice(12);
The variable sixSided
is assigned a new six sided Dice
object.
The variable twelveSided
is assigned a new Dice
object that has 12
sides.
Behaviors Defined Through Methods
A method is a block of code that has been defined to perform a specific task or algorithm. Methods can receive arguments making them available to use in the algorithm. Methods can have a return value to provide a result of the algorithm.
Methods can be:
- static methods which are methods that belong to the class and should be called using the class name instead of an object.
- instance methods which are methods that belong to the object and are called using an object of the class.
A method has three main parts:
- return type, or
void
, if the method isn't providing a value. A return type is the data type of the value that is the result of calling the method. If the method performs an action and doesn't result in a value, the return type is void
.
- the name of the method. Typically starts with a lowercase letter and adheres to the naming rules for variables.
- parameter list in parenthesis (this list can be empty). The parameter list allows arguments to provide additional data the method needs to complete it's task.
The following pattern is used for non-void methods. There can be no parameters listed:
dataType methodName(dataType parameter1, dataType parameter2, ...)
The following pattern is used for void methods. There can be no parameters listed:
void methodName(dataType parameter1, dataType parameter2, ...)
Example Dice Methods
The Dice
class has three methods.
- int numSides() - returns the number of sides the
Dice
object has
- void setSides(int sides) - changes the number of sides of the
Dice
object
- int roll() - returns a random value between 1 and the number of sides of the
Dice
object.
Calling Methods
Calling Void Methods
When there is no value being provided as a result of calling the method, the return type of the method is void
. These methods are called as follows:
objectName.methodName(argument1, argument2, ...);
If this is a static method, the class name is used instead of the object name.
If there are no parameters in the parameter list, the argument list will be empty.
Example Dice Method Call
Dice myDice = new Dice(); //creates a Dice object with 6 sides
myDice.setSides(10); //sets the number of sides for this dice to 10
Calling Non-Void Methods
Non-void methods result in a value. This value can be captured and used.
- One way that we capture the value, is through assigning a variable to the value returned. The variable needs to be an appropriate type to store the value, so typically the same as the return type.
- The value can also be used in an expression.
- The value can also be passed as an argument to another method, including
print
and println
, which will display the value.
dataType variableName = objectName.methodName(argument1, argument2, ...);
Example Dice Method Call
Dice myDice = new Dice(); //creates a Dice object with 6 sides
int value = myDice.roll(); //value is assigned a random value between 1 and 6.
Resources