Objects have special kind of method called a constructor. A constructor is an optional method that is called when a new instance of an object is created with the new keyword. Constructors are used to initialize the fields of the new object instance. A constructor looks like a method except that it has no return data type (including void) and has the same name as the class. A constructor can have a parameter list and a class can have more than one constructor by varying the parameter list.

Introduction to Objects

Java is an object-oriented language so it stands to reason that objects are the workhorse construct of the language. We have talked about objects and classes in previous lessons but now its time start our detailed exploration of objects.

An object is a software container that includes state information (variables) and behaviors (methods). Objects are used to model real-world things like cars, people, bank accounts, etc. Joysticks, controllers, Motors, servos, and sensors are all typically going to be objects in robot code.

Method Wrap-up

We are going to wrap-up the unit on methods with several resources for you to view:

Here is a video about methods. This video is a good introduction to methods. However the definition of the static keyword in this video is incorrect. Please ignore the discussion of static for now as we will explore static later in this curriculum.

Passing Reference Parameters

In the lesson on passing parameters we learned that primitive parameters are passed by value, that is, a copy of the calling code's variable value is given to the method for it's internal use. Reference variable (object instance pointer) parameters are passed the same way but there is an important distinction to make in how reference parameters (objects) are used in methods.


Remember that we mentioned that a method's name and parameter list are called the method's signature and this signature is how Java identifies methods. So it follows that while methods with a different name are definitely not the same method, methods with the same name and different parameter lists are also not the same method. This allows you to use different parameter lists to create variations of a method single name. This is called overloading. Here is an example of overloading:

Passing Parameters

Parameters (also called arguments) are the way in which we can send data (variables or constants) to a method for it to act on. While there are cases where methods may not need parameters, parameters make methods especially useful. Parameters allow a method to produce a different result each time it is called depending on the input values supplied. Parameters are said to be "passed" to the method.

Method Structure

The form of a method is:

The components are:

What are Methods?

Methods are reusable code blocks that are only executed when called from another location in your program. Methods are also called functions or subroutines. For our purposes, these names mean same thing. In other languages, the names may mean something a bit different.

FOR Statement

The for statement is used to execute a block of statements some specific number of times. The general form of the for statement is:

SWITCH Statement

The switch statement allows you to compare a value to a list of possible matching values and so select from multiple possible statements or blocks. A key difference from the if statement is that the value compared is not boolean, but instead numeric, enumeration (discussed later) or String. The form of the switch statement is: