If you read the article linked in the previous section, you would have read that a class is a blueprint for creating an object. For example, we want to create student objects. The Student class would define what such an object contains.

Let's get started. In your Project pane, right click on the folder with your source files (mine is com.example.kannan.testapplication) and select New > Java Class.

On the next screen, we fill in the class name as Student. The other fields can be left blank.

This should create Student.java. The contents of this file are relatively sparse:

public class Student {

}

(I have omitted any automated comments and the package name that appear at the top).

Even though there isn't much on the page, we have declared a class. The class keyword, followed by the name, declare the class. The curly brackets that follow denote a block of code that we will fill out soon.

Since we have a class, let's go learn how to use to make an object. Head back to MainActivity and write this:

Student student;
student = new Student();

The first line is a variable declaration. Since we've declared the Student class, Student is now a type that is available to us, like String or int. The second line creates the new object and assigns it to the student variable. We've briefly seen this syntax before, where I referred to it as a constructor. The new keyword, followed by the class constructor Student(), creates a new student object.

Recap: We created the Student class, which is a blueprint of what defines a student. We then used the constructor to create a new Student object.

Now, there isn't much we can do with this class right now, since Student.java is almost blank. Let's go back to Student.java to add something.

We want each student to have a name. A name can be stored in a String variable. So what we want is for each student to have a String variable called name. Write this in the Student class:

public class Student {
    String name;
}

We've declared a new variable that belongs to each Student is created. This is called an instance variable, because it belongs to the specific instance of Student that we create. Each Student will have a different name.

Back in MainActivity.java, we can add an additional line.

Student student;
student = new Student();
student.name = "Bob";

We've assigned to the name variable. The "." dot syntax allows us to access things that belong to an object. In this case, we have accessed the name variable, and assigned to it.

You might have noticed the autocomplete dropdown pop up when you hit the dot. This is a great way to discover classes that you did not write. For example, when printing to the console, when you type "Log.", you will see options other than d pop up, which you can look up online to find out what they do.

Now that we've assigned to this student's name, we can use it like any other variable. For example, we can print it back out:

Log.d("MainActivity", student.name);

Try: Create another student object and assign the name Susan to it.

Your solution might look like this:

Student student1;
student1 = new Student();
student1.name = "Bob";

Student student2;
student2 = new Student();
student2.name = "Susan";

As you can see, each instance of Student can have a different name. This allows us to organize all student related information in one class.

Try: Add age and phone number to each student as well.

Remember, just writing student1.age = 18 is not going to work. You will get an error complaining that age is unrecognized. This is because you have not yet declared age in the Student class. Until you do, each student created using that blueprint won't have an age.

Your solution might look like this:

public class Student {
    String name;
    int age;
    String phone;
}

Let's say we want to print the name of every student. We might have something like this:

Log.d("MainActivity", student1.name);
Log.d("MainActivity", student2.name);
Log.d("MainActivity", student3.name);

This is repetitive, so we probably want to use loops. If we want to use loops, we shouldn't have separate variables for each student. We should be using an array of Students.

Try: Make an array of students, and write a loop to print each student's name, age, and phone number. Hint: we've seen how Student is a type just like int or String. So an array of Student would be declared and used similarly.

Your solution might look like this:

Student[] students = new Student[2];

students[0] = new Student();
students[0].name = "Bob";
students[0].age  = 18;
students[0].phone = "12345678";

students[1] = new Student();
students[1].name = "Susan";
students[1].age  = 19;
students[1].phone = "12345678";

int counter = 0;

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

    counter++;
}

Notice how Student is used as a type just like int would be. Also notice how we use square brackets [ ] to access a single student object, and then use dot syntax to get the name or age. You might have tried something like students.name[0]. But look closely at that line; it seems to imply that students is an object that has a list of names. What we actually have is a list of students, who each have a name.

Note: instance variables are sometimes referred to as properties or fields. You might see these terms used interchangeably when you search online. There are some subtle differences if you're interested: http://stackoverflow.com/questions/10115588/what-is-the-difference-between-field-variable-attribute-and-property-in-java. But they are often just used interchangeably.

-

results matching ""

    No results matching ""