Introduction to Java Static Variables: Android Studio Crash Course (Free Tutorial)

Ready to master Java? Today’s topic is static variables. Static variables extend across the entire run of a program. In this article, you’ll learn how to create static variables in Android Studio with Java.

If you need an introduction to coding and want to learn for FREE, check out our 30-minute beginners course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes

A static variable is associated with a class rather than a specific object. So let’s create a class! To follow along with this example in Android Studio, go into Project view. Then go to app > java. Right-click on the topmost com.example.zebra.demo. Select New > Java Class.

The tab “Create New Class” will pop up. Let’s name our class “Vehicle”. Follow this format:

public class Vehicle {

}

As you can see, we have created a new class called Vehicle. Next we shall set up some fields. On a line within the curly brackets of the public class, let’s declare a string brand.

private String brand;

Note that we made this field private, meaning that we can access the variable only inside this Vehicle class.

Furthermore, let’s declare a variable for the conversion rate from kilometers to miles, which is 0.621371.

public static final double KILOMETERS_TO_MILES = 0.621371

First off, the name of our variable is KILOMETERS_TO_MILES, and its value is 0.621371. We used several keywords to specify the type of variable:

  • public is used because the static variable has to be used throughout our project.
  • static is used because our conversion rate applies to all the vehicles. The rate is not specific to a certain vehicle, such as a Honda or Toyota.
  • final is used because the conversion rate is a constant that won’t change throughout the project.
  • double is used because our conversion rate has decimal places. A variable of the data type double can contain decimals.

Now we have to create a constructor for our class. Constructors are run whenever objects are created. On a new line, type in:

public Vehicle(){

}

Within the parentheses, set up the parameter String brand. As well, within the curly brackets, say:

this.brand = brand;

This means that our input field brand is equal to the value of the parameter brand.

To make sure we can retrieve brand, create a getter by coding:

public String getBrand(){
return this.brand
}

This is called the getter method because it “gets” the private field. We can’t access the private field String brand otherwise.

Now that we have created the constructor, go to app > java > (topmost) com.example.zebra.demo > MainActivity. Beneath setContentView(R.layout.activity_main);, create a new Vehicle called myVehicle. Give it the value Toyota:

Vehicle myVehicle = new Vehicle("Toyota");

We just created a variable! We also want to display a message on the screen. For this to happen, on a new line, type in “Toast”, select “Create a new Toast”, and hit Enter. Android Studio will auto-complete the following code:

Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT).show();

To specify the message we want to display, edit that code so it looks like this:

Toast.makeText(MainActivity.this, "Brand of my car: " + myVehicle.getBrand() + " Conversion rate between KM and MILES: " + Vehicle.KILOMETERS_TO_MILES, Toast.LENGTH_SHORT).show();

Note the difference between myVehicle.getBrand() and Vehicle. myVehicle is used because getBrand applies to a specific vehicle. When attaching the variable for the conversion rate, Vehicle is used because the conversion rate applies to all vehicles.

Run the emulator and zoom in. You should see the message “Brand of my car: Toyota Conversion rate between KM and MILES: 0.621371”.

If you want to learn even more coding for FREE, check out our 30-minute beginners course here: training.mammothinteractive.com/p/learn-to-code-in-30-minutes