STEM Robotics 4 All

STEM Resources for all

Basics of FTC Java and OpModes

Welcome back for Lesson 2!

What you will learn by the end of this lesson:

  • Basic Java
  • OpMode structure and examples

Note: if you already know how to program in Java, skip to the second half of this article.

Crash Course into Java

Java, like most programming languages, has variables of either primitive (int, double, float, boolean, etc) or Objects. To declare a variable, you write following:

int a = 0;

You can replace int with any primitive or object type to declare that kind of variable. The variable name needs to start with a letter, and after that can be alphanumeric. They are case sensitive!

Note: don’t forget the semicolon 🙂

There are actually 3 types of variables: local, instance, and class. Local variables are defined as in the example above, instance variables are accessible in an object created from a class, and class/static variables live in a class but can be accessed anywhere if marked as public.

Objects can be thought of as a model for something with properties and behaviors (fields and methods). Classes are blueprints for objects. They outline the fields and methods. Classes can be built on each other, either inheriting (copying everything and either overriding or creating methods/fields) or implementing interfaces, which are outlines for classes, describing methods that must exist.

Declaring a class looks like this:

1  public class myClass {
2      public int a = 0;
3      public static double square(double num) {
4          return num*num;
5      }
7      public myClass(int a_input) {
8          this.a = a_input;
9      }
10 }

In the code above, you can see several things at work:

  • The class declaration is the first line, telling the compiler where the start of the blueprint is.
  • The next line describes a instance variable a of type int with initial value 0. This variable is created and destroyed with object instances, and cannot be accessed without having an object.
  • The third through fifth lines declare a static method, which belongs to the class but not an object of the class. To call this method outside this class, you must write myClass.square(num) where num is of any primitive type except for boolean and byte, or is of type Double (wrapper class). What it does it quite self explanatory: just returns the square of its input num
  • The seventh through ninth lines define the constructor of myClass: a special method which returns a new instance of the object described in the class. It should be called as following:
    myClass instance = new myClass(4);
  • The last line (10) simply contains the bracket to end the class declaration.

Note that any class you write with public before it must be in a file of the same name. So the class above (myClass) needs to be in a file called

To use the class myClass in another folder (but the same project), you need to import it via the import command: import package.myClass;

Classes can inherit from other classes using the extends keyword. This makes a class copy all methods and fields in the superclass (the class from which we are inheriting), but you cannot access/call private marked fields/methods. You can also mark some methods as abstract to force the inheriting class to write code for a specific outline. The OpMode class uses this to force you to write init and loop code for your robot.

Any OpMode you write must extend (or inherit from) the root OpMode class, making sure that it has the methods necessary to tell the robot what to do.

OpMode structure

All OpModes must inherit from the root class OpMode, which is in the FtcRobotController module of the Android Studio project. Thus, you cannot change it (you can, but it is illegal). The video below shows how to create a sample opmode:

Or a Linear OpMode:

As you saw, clicking the lightbulb icon in Android Studio automatically provides suggestions for how to fix egregious (or really bad) errors.

The structure of OpModes contains two main parts (and other optional ones): The “init” phase and the “loop” phase. The “init” method is where you initialize all your hardware and variables, such as PID constants and motor/servo/sensor objects and configurations. The “loop” method is called extremely fast (50 times per second (50Hz)), and here you can do checks for gamepad buttons, adjust motor power, read sensor values, etc. This is where you will have your primary performance code.

Linear OpModes are extremely similar except for the fact that they have only one method: runOpMode.
Your init code goes in the beginning of that method, then you call waitForStart(); to end the init part, and then for the loop you call while(opModeIsActive()) {} and put your loop code inside the brackets.

Which one to use is really a matter of preference, but linear opModes are much more flexible and slightly more advanced, so we recommend starting with OpModes and moving to LinearOpModes as you learn more about FTC programming.

In the next lesson we will cover a simple RC controller OpMode as an example.

© The RoboMentors (Marc and Anne-Sarah)

%d bloggers like this: