Introduction to Java Class Constructors: Android Studio Crash Course (Free Tutorial)

Picture this: you have a user that wants to specify the details of a product. Today we’re going to be looking at a user who inputs the make, model, and manufacturing year of their car, and we want Java to display the inputs as a message.

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

When an object is created, Java first executes the code in the constructor. The constructor will then invoke that object. In this example, we’ll look at both default and customized constructors in a Java class. So let’s create a class! To follow along 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 “Car”. You should see the following on your screen:

public class Car {

}

As you can see, we have created our new class. It will also appear in Project view inside the same folder that contains the MainActivity class.

Before we create constructors, we have to instantiate a couple of fields. In the public class Car, let’s declare some fields. Note that, unlike in this example, these fields are usually private, meaning that they can only be accessed in this class. In your public class, create two public strings named make and model. Also create a public integer variable for the year in which the car was manufactured. Name it yearManufactured. Note that each of these lines must end in a semi-colon.

public class Car {
public String make;
public String model;
public int yearManufactured;
}

Now that we have created some properties for the car object, we have to create a constructor. First let’s trying using a default constructor. Note that a constructor takes the same name as its class. Still within the public class, type the following:

//default constructor
public Car (){

}

Instantiate the fields by adding onto the constructor so it looks like this:

//default constructor
public Car (){
this.make = "";
this.model = "";
this.yearManufactured = 2000;
}

For string variables, we usually put "", signifying empty strings. For integer variables, we usually use 0. In this example, we used 2000 because it’s a much more appropriate default value for the year in which a car was manufactured.

Next go to app > java > (topmost) com.example.zebra.demo > MainActivity. Beneath setContentView(R.layout.activity_main);, create a new object called myCar. Use the default constructor Car:

Car myCar = new Car();

We also want to display a message on the screen. To set the message, on the next line, create the string message, and set it equal to the properties that we created in the Car class.

String message = "Make: " + myCar.make +
"\nModel: " + myCar.model +
"\nYear manufactured: " + myCar.yearManufactured;

Note that because we added \n to the last two lines, our message will be displayed on three separate lines.

To display our message, we can use the Toast utility in Android Studio. On the next 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();

Run the emulator, and zoom in. Because we used the default constructor, you will see that the defaults we created will be shown on the screen:

Make:
Model:
Year manufactured: 2000

There are no values for the make or model of the car since we didn’t specify their values when we created the Car class. So let’s specify their values, shall we? To do so, we must create a customized constructor back in the Car.java tab.

Below the default constructor, add a customized one:

//customized constructor
public Car(){

}

The only difference between the default constructor and the customized constructor is that we can pass some parameters in the parentheses of the customized constructor. Let’s add these parameters into the parentheses: String make, String model, int yearManufactured.

These are the parameters that the users of our application will input. For instance, a user will specify the make of a car, such as Honda, then the model, such as Civic, and the year manufactured, such as 2013. Since these are specific values, we have to make sure that our fields store these values. In the customized constructor, type the following lines:

this.make = make;
this.model = model;
this.yearManufactured = yearManufactured;

This code means that your field that stores, for example, the make is equal to the make that the user inputs in the initialization of the object that we just specified in the parentheses.

Let’s see how this customized constructor works. Instead of having the Car instantiate the constructor without any parameters, we’ll specify for the make, model, and manufacturing year of a car. In the parentheses after Car myCar = new Car, type: "Honda", "Civic", 2013.

If you run the emulator, you will see the following:

Make: Honda
Model: Civic
Year manufactured: 2013

All done! Now we’re able to store specific values inside fields. 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

Mammoth Interactive Favicon

Why you NEED to take this course :

Get in Touch.

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

EMAIL US

support@mammothinteractive.com