Skip Top Navigation Bar

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.

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:

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:

A method has three main parts:

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.

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.

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