A class can have more than just variables. We can also have methods (or functions). To see why this might be helpful, consider this example.

When printing the details of a student, we might do something like this:

int counter = 0;

while (counter < 2) {
    Log.d("MainActivity", students[counter].name + " (" + students[counter].age + ") - " + students[counter].phone);
    counter++;
}

We might want to use a nicely formatted string like this in multiple places. Of course, we can make a function to make this reusable:

public String studentDescription(Student student) {
    return student.name + " (" + student.age + ") - " + student.phone;
}

And use it like:

int counter = 0;

while (counter < 2) {
    Log.d("MainActivity", studentDescription(students[counter]));
    counter++;
}

One thing of note: Notice that I didn't make a printStudentDescription function to directly print. That is a reasonable way to do things, but this way I can use the string in other ways (for example, display in a TextView). When making functions, it can be worth thinking about which specific part we want to spin off into a function to make it most reusable.

This works nicely. But something about this still feels off. Remember that in programming, we like when things are structured well. Lists go in arrays. Related information gets grouped into classes. So over here, we have a function that turns a student's information into a description string. This is entirely to do with students. So what is this code doing in MainActivity.java? In an activity, we want to primarily be doing things that have to do with the user interface. Putting the code here also means that if we want to do this in another activity, we have to define the function again!

What would be great is if the Student class could just hold this code. This is what we can do with instance methods.

Instance methods are no different from the functions that we have learnt till now. The only difference is that these functions belong inside of classes that we are creating.

Go ahead to Student.java and add this function inside the class:

public String description() {
    return name + " (" + age + ") - " + phone;
}

There's a few key differences here from what we did earlier. First, the name is shorter: since we're already in the Student class, the word "student" is now redundant. Second, we have removed the input argument. We are already inside of a specific student object; this means we already have access to name, age, phone, etc. Third, we don't need to keep writing "student.name", because this "name" just refers to the instance variable we declared above. Moving the code to its rightful place has made it cleaner and easier to understand. When called, this function returns this object's description.

To call the function, we can do something like this:

int counter = 0;

while (counter < 2) {
    Log.d("MainActivity", students[counter].description());
    counter++;
}

Notice that description(), just like instance variables, is accessible using dot syntax. This is a function that belongs to that student object. So, it is going to use the instance variables that also belong to that object when running.

Whenever you write functions, ask yourself where the function should live. Quite often, writing the code inside the relevant class can vastly simplify your code. Just compare what we have here with the first code sample on this page. The code is now cleaner, and can be used in other activities as well.

Try: Add an instance method that says if the student is below 18.

Hint: What kind of question are we asking: Is the student above 18? It's a yes/no (true/false) question. So what should the return type of the function be.

Your solution might look something like this (in Student.java):

public boolean isAdult() {
    return age >= 18;
}

Note the way I've named my function; this is a common convention for naming functions that return boolean values. It's nice, because the resulting usage almost reads like English:

if (student.isAdult()) {
    letThemIn();
}

-

results matching ""

    No results matching ""