Customization Description: 

This course is intended to introduce the Java programming language to students using the EV3 (FLL), Tetrix (FTC) and RoboRio (FRC) robotics platforms. For EV3, the course moves the student away from block based robot programming to using a text based programming language. For Tetrix and RoboRio, the course will provide more instruction in Java itself, which is missing in existing materials. The course will teach a basic competency in Java with a focus on robotics applications. Robot construction will not be covered in any depth as it is assumed the student will have or acquire hardware building skills separately. The course is targeted to beginners and there are no prerequisites.

Get started using this course by clicking the first Unit and then the first Lesson. The Lesson content will be displayed and next/previous lesson buttons will appear at the bottom of each lesson making it easy to move between adjacent lessons.

Education Level: 
Learn about Interfaces which are definitions of the fields and methods implementing classes must expose.

Understand Interfaces and be able to use them when appropriate.


Just as a class is a description of an object, an Interface is a description of a class. An Interface defines the fields (variables) and methods that a class must provide (public access) to users of the class. The Interface does not define how a class derives the value of a field or the result of a method, only that a class that implements an Interface must expose that Interface's fields and methods. As such, the Interface defines a public facing API that the class must provide. Interfaces are also called Contracts, in that the Interface defines a contract or agreement (in terms of the fields and methods exposed) between a class and users of that class. Classes may extend only one (parent) class but classes may implement any number of interfaces.

One power of Interfaces is that any class that implements an Interface can be used where ever that Interface is defined as a required data type. This concept is best explained by an example.

A simplifed view of the PIDController class in the FRC library shows a constructor that takes two classes as input, a PIDSource object and a PIDOutput object. PIDSource and PIDOutput are both Interfaces. Digressing for a moment, a simplified PIDController needs two things to perform its function. An input or process control value that will be used by the PIDController to compute an output value, which must be sent to some other class for action. Now in many cases the input value will come from an encoder (counts) and the output value will go to a motor (power). You could define the PIDController class constructor as Public PIDController(Encoder enc, Motor motor). There are two problems with this. First, the PIDController class and the Encoder and Motor classes have not agreed on how data will pass between the Encoder and PIDController class and how data will pass between the PIDController and Motor class. The second issue is that the PIDController as defined will only work with Encoders and Motors. What if we wanted to use some other classes as the input and output objects? Interfaces solve both of these problems.

The simplified PIDController class constructor actually looks like this: Public PIDController(PIDSource source, PIDOutput output). PIDSource and PIDOutput are Interfaces and they define what fields and methods are required for any class that wants to act as a PID source or PID output object. These Interfaces define the contract between the PIDController class and any class wanting to act as a PID source data provider or a PID output data consumer. The example Interfaces look like this:

A class implementing the PIDSource Interface must provide a method defined as double pidGet(). That method must return the current input value to be used by the PIDController. Since any class passed into the PIDController constructor for input must implement the PIDSource Interface, the PIDController now knows what method to call on the source object reference variable to get the input value. For instance, the Encoder class implements PIDSource and as such must provide the method pidGet() along with its other methods. When pidGet() is called, the Encoder returns the current tick count. So an Encoder object can be passed to the PIDController constructor. But so can any other class that implements PIDSource. A class implementing the PIDSource Interface would look like this:

Any class that implements PIDOuput must provide the pidWrite(double value) method. That method when called, will take the passed value and perform some action with it. The PIDController knows that any class implementing PIDOutput will have  the pidWrite() method and so it knows how to send the output value it has calculated from the input, by calling the pidWrite method on the output object reference variable. A class implementing the PIDOutput Interface would look like this:

For instance, the Motor class implements PIDOutput and provides the pidWrite(double value) method and sets it's motor power from the value. So a Motor object can be passed to the PIDController constructor as an output object. But so can any other class that implements PIDOutput.

A simplified PIDController class would look like this:

An example of using these classes:

The key here is that the single implementation of the PIDController class can handle both the custom source and output classes as well as the standard encoder and motor classes.

Classes can implement more than one Interface along with any other fields or methods they wish. Interfaces are very powerful and allow a class expecting an Interface (like PIDController) to work with any number of other classes that implement that Interface.

Note that Interfaces only define the expected fields and methods to be exposed by the implementing class. The actual implementation (code) is contained in the class implementing the Interface.

Like many things in Java (and other languages), this is a basic introduction to Interfaces. There is a lot more to Interfaces which you can read about here. Here is a video on interfaces. This lesson should be enough to understand Interfaces and use them in your robot code.



Explore some of the advanced data types available with Java.
Explore the Java concept of class packages and using packages with the Imports statement.

Understand the Java concept of class packages and using packages with the Imports statement.


A real project, even robotics projects, can end up having many classes. You will likely need to use classes in libraries provided by FIRST or others. Finding the classes you need, controlling access to them and naming conflicts between your classes and library classes would be a problem. You may also want to organize a group of your classes into a library that can be reused with other projects or other robotics teams. Packages give us a way to group together related classes in a unique naming scheme and reference them from other classes.

Packages are intended to group related classes together and facilitate access control and prevent naming conflicts. Naming conflicts would occur if all the library classes you use and all of your classes were all grouped together, and there are library classes that have the same names as your own classes.

You create packages with the package statement placed at the start of your class files. The package statement has your package name (more on names later) and all classes with the same package name in your project are grouped into that package. All Java programs must have a package and If you don't use a package statement, your classes are grouped into the default "unnamed" package. It is good programming practice to at least put all of the classes in a project into the same directly named package. Here is more about packages.

The Java built-in library of classes is organized into the java package and there are many sub-packages organized by function. A format is used to create a hierarchy within packages. Here is a detailed discussion of package naming.

While a specific package name format is not required there is a detailed convention used in package names. For robotics, you might want to use a name format like this:   


or a simpler but perfectly valid one:


The first example locates your project package in relation to the world. Your package would not conflict with any other package if named like this. This is a nice convention but only important if your code can be integrated into other peoples projects and you plan to do so. The second example shows a simpler package structure that works fine for projects not shared with anyone else.

Packages organize classes we want to make use of in our own classes. So how do we access these other classes? The types (another name for classes) in a package are referred to as members. To access the members of a package you can specify the fully qualified package name when using classes, fields or methods or you can use the import statement. Using fully qualified package names can get pretty tedious. The import statement makes the package name known to the compiler so that when the compiler finds a class, field or method name it does not find in your code, it uses the imported packages to look for the name. This is called resolving the reference. Here is a discussion on using the import statement. Please read this discussion as it covers important details not discussed here.

Robot programs will import some number of Java packages and some number of packages from the robotics library provided for your hardware platform. You will see examples of this when you get to the unit covering your hardware platform.

Note that you do not see any imports or package name in the CodingGround examples. CodingGround uses the default package and is doing any needed imports for you behind the scenes.

Finally, many Java compilers and IDEs want to organize your source files (.java) into directories based on the package name(s) you use. Typically a directory hierarchy is created by IDEs that matches the package hierarchy when you create a package in an IDE. So the package name suggested above might have the directory hierarchy:




Note that project name is repeated because the projectname right after the c:\ is not part of the package, but is part of the project directory hierarchy.

Here is a summary discussion of packages and import. Here is a video on this topic.

One more important aspect of this topic. With import you tell Java what packages or classes within packages you want to use. But where does Java actually find that code to include in your program? When a programmer creates a library of classes, (a package) the deployment step for such a project is to create a .jar file. Jar means Java Archive and is a special zip file containing the byte code for the classes packaged into the jar file. You tell your IDE what jar files are available on your computer and then when you use an import statement, Java can locate the code to import in the jar files it knows about through your IDE. Typically, one part of an SDK is all of the jar files needed to develop your programs. Your robot platform SDK contains the jar files you need and automatically configures your IDE to know about them. At some point you may wish to use a package of classes not included in the robot SDK. You would download the jar file containing the package and tell your IDE about the jar file and then you can import members from the package.



Explore the difference between passing primitive parameters and reference parameters.

Understand the difference in behavior of primitive data type parameters and reference data type parameters in methods.


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.

For this lesson, we are going to use the String object and a new object called StringBuffer. StringBuffer is part of the standard Java API library. We will use String and StringBuffer to illustrate show Java manages object instances and reference variables.

 String objects are immutable, meaning that once they are created they can't be changed. So if we do this:

The first statement created a String object instance with the value "abc". Java allocates computer memory and places a String object containing the characters abc into that memory and places a reference (pointer) to that memory location in the variable myString. The second statement creates a new String object instance with the value "xyz" and places a reference to that new object in the same variable, myString. At this point the reference (pointer) to the original object containing "abc" is now no longer available to us. "abc" still exists in memory but we overlayed the reference to "abc" with a reference to "xyz". The String object "abc" is now orphaned, meaning it no longer is connected to a variable in our program and will be discarded by Java. This is called garbage collection.

Note that the String object has no methods that change it's contents. Strings are modified by making a new String object from the old one. Here is another example:

Here again, we did not modify the string "abc" with the second statement. A new string is created containing the characters myString references, "abc" in this case, and concatenates the string "xyz" to create a new string containing "abcxyz" and a reference to this new string is stored in myString. The original string containing "abc" is no longer referenced by this code and will be garbage collected.

Now the StringBuffer class does provide methods to change it's contents without creating a new object. So if we do this:

The first statement creates a new StringBuffer object instance and places "abc" into it. The second statement calls a method on the StringBuffer object that adds "xyz" to the "abc" already inside the StringBuffer object instance without creating a new instance. When done, mySB is still a reference to the same StringBuffer object which now contains "abcxyz".

So with all of that out of the way we can discuss the nuances of using reference parameters in a method.

Consider this example:

The intent of the method is to append "xyz" to whatever is in the String str. After the call to myMethod(), what is in the String myStr? The answer is "abc". Why? The parameter myStr is passed as a copy and placed into the method variable str. In the method, we change the contents of str. Str now points to a new String object instance containing "abcxyz". However, this new String object instance reference is not passed back to the calling code. myStr still points to "abc". Now consider this example:

After the call to myMethod1(), what does mySB contain? The answer is "abcxyz". Why? Because we called a method on the mySB object instance using the copy of mySB passed into the method's sb variable. This means that sb also references or points to the same object instance as mySB. With the StringBuffer object's append method, we change the internal state of the object instance jointly pointed to by sb and mySB.

After the call to myMethod2(), what does mySB contain? The answer is still "abcxyz". Why? Because we created a new StringBuffer object instance containing "xyz" and stored the reference to it in sb. For the rest of the method sb contains a reference to "xyz". Since we changed the object instance that sb points to, the method lost its pointer to mySB and can't reference it after that point. Note that at the end of the method sb, which contains "123456", will be released since variables created in a method only exist while that method is executing.

As a final point, in our first example the method wanted to append "xyz" to whatever string was passed into it, but it does not change the string that was passed in. Here is how you could fix that method:

Here the method concatenates the two strings using the + operator and creates a new string with it's reference deposited into str. The method then returns this new string reference and the calling code replaces the original string reference with the returned string reference and so myStr ends up pointing to the string instance "abcxyz".

A final note about garbage collection. In the last example, str will go out of scope when the method ends. You might think the new string pointed to by str would be orphaned and then garbage collected. However, since a reference to the actual string object instance in memory was passed back to the calling program and placed into myStr, the string object is not orphaned and will continue to exist. When a reference to an object instance is stored in a variable, the JVM keeps track of that by incrementing the object instance's reference count. When a variable that references an object goes out of scope or the variable has something new stored in it, the JVM decrements the object instance reference count. This is how the JVM knows when an object instance is orphaned, when its reference count goes to zero (no variable is pointing to it). When no variable is pointing to an object instance in memory, that object instance is no longer accessible and the memory can be released.

Here is a video about the difference between primitive and reference variables and how they are passed to methods.

Here are the examples on CodingGround.



Explore general topics related to using objects in Java programs.

Understand additional basic concepts related to using objects in Java programs.


Now we are going to look at some aspects of using objects in your code.

The first topic is access-modifiers. An access-modifier is one of the modifier keywords that can be specified before the word class when defining a class, before the Data Type when defining a variable and before the return Data Type when defining a method. We have been using public for access modifiers thus far. An access-modifier determines what access other classes have to the components of your class. There are several different access-modifier keywords but for robotics projects, public and private are sufficient. For classes, always use public. This means other classes can access your class.

When defining class level fields (recall that fields are the internal variables or ‘properties’ that an object may have), you can use public or private. For fields, public means other classes can access the field directly. Private means the field may only be accessed by code in your class. For robotics, fields can be public or private, but good programming practice says fields should be private, that is, only accessible outside your class through methods.

When defining methods, you can use public or private. For methods, public means other classes can call the method and private means the method can only be called by other methods in the same class. For robotics, methods can be either public or private as you think best. Private methods are those that support your class and don't really make sense to call from other classes. Here is a video about access control and here is more information about access control.


The next topic is object instance lifetime. When you create an object instance with the new keyword, a block of memory for the class variables is allocated in computer memory and a pointer to that memory location is placed in your object variable. The fact that there is a pointer to the instance in memory is recorded (called reference count). Any additional pointers to the same instance increment the reference count. As long as the reference count for an object instance is not zero, the instance will  be maintained in memory. When the reference count goes to zero, no one is using the instance and the JVM marks the memory as garbage and it will eventually be reclaimed by the system. The reference count is decremented whenever a variable pointing to an object instance is deleted (goes out of scope) or the value of the variable is changed to point to some other object instance or no instance all (null).


The next topic is variable scoping. Scoping describes the lifetime of variables you create in your program. When you define variables in a class at the class level, those variables will be allocated memory when a class instance is created with the new keyword. As long as the class instance is in use (reference count not zero), those fields will exist in memory. When a class instance is not being used anymore (marked for garbage collection), all of it's variables will have their memory released as part of that process.

Variables defined in methods (local) only have memory assigned to them when the method is executing. When a method starts, the local variables are allocated memory and retain it while the method runs. When the method ends, the memory is released. When a variable's memory is released because it's containing method or class ends it is called going out of scope.


Our next topic is the this keyword. The this keyword refers to the current instance of the containing object. The this keyword helps resolve ambiguity in naming within a class. In a constructor or method, if you use a variable or parameter name that is the same as a class field, the method local name hides the class field of the same name. The this keyword allows you to access the hidden name. Additionally, when calling a method which has a parameter which is a reference to the instance of the calling class, this allows you to pass the calling instance. Here is an an example of that:

This may seem a strange example but in reality there are many cases where a method in another class needs our own class instance as a parameter. Here is more information about the this keyword.



Explore object constructor methods.

Understand what an object constructor is and how it is used.


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. Lets look at our Dog class from the previous lesson:

We see that the fields breed and name are not initialized. This would lead to a run time error (called an exception) if we called the bark() method without first putting something in the breed and name fields. We can fix this with direct initialization of the fields or with a constructor method, and we will add a second constructor that allows us to set the breed and name fields when we create a new Dog object:

 So in another place in your code you would write:

We have added three constructors, the first with no parameters which would create a Dog object with the breed field set to an empty string. We directly set the name field to an empty string. When we create an instance of this class, the bark() method won't fail because the constructor is called and initializes the breed field and the name field is initialized when the object is created by Java. If we use the second constructor when we create the Dog object instance, the breed field is set with our desired value. If we use the third constructor when we create the Dog object the breed and name fields are set to our desired values on one statement.

If you omit a constructor, the Java compiler creates one for you internally. That default constructor has no parameters and no code so it really does nothing.

Note that the second constructor shows the use of the this keyword again and also how you can do it without using this.

Here is a video about constructors.

Here is the example on CodingGround. Modify the example to add an age variable (field) and a constructor that allows you to initialize the age (along with breed and name) when creating a new Dog object instance.