Topic 1 - Variables and Data Types
Topic 2 - Conditionals and Strings
Topic 3 - Loops
Topic 4 - Arrays
Topic 5 - File Handling
Semester 1 Projects
Topic 6 - Classes/Objects and Methods
Topic 7 - ArrayLists
Semester Projects

Object-Oriented Example

It’s best if we create a class that is useful, as opposed to the ‘FirstClass”/”SecondClass” examples we’ve shown so far. We’ll build a Person class.

We’ll start with the definition of the class:

public class Person
{
}

Fields/Instance Variables

This class is empty so far and it has no main method so it can’t do anything, not even run!

Next, we’ll think of what attributes a person could have. Since we’re in charge of building this Person class, then we can set whatever attributes we want. We’ll call these attributes fields or instance variables. In this case, our person will be able to hold data for its name and its cell phone number.

public class Person
{
    /// fields (aka instance variables) ////////////////
    String name;
    String cell;
}

Methods/Functions

We also want our Person to be able to DO stuff. These would be a way to show actions with code. Just like you wrote functions in Python to organize a set of instructions neatly, we’ll do the same with methods.

public class Person
{
    /// fields (aka instance variables) ////////////////
    String name;
    String cell;

    //////////// methods ///////////////////////
    public String getName()
    {
       return this.name;
    }

    public void setName(String theName)
    {
       this.name = theName;  //set FIELD to new value
    }

    public String getCell()
    {
       return this.cell;    //return VALUE of field
    }

    public void setCell(String theCell)
    {
       this.cell = theCell;
    }

    public String toString() 
    { 
       return "name: " + this.name +", cell: " + this.cell; 
    }
}

You’ll notice that our methods don’t do anything particularly interesting. Each of those methods just returns the value of the fields in the class or sets the value of the field to a new value.

Methods that change the value of fields(aka instance variables) are called mutator methods (aka getters).

Methods that return the value of fields(aka instance variables) are called accessor methods(aka setters).

Constructor

A constructor helps us initialize the values of variables. It’s a convenient way to say, “When this object is created I want it to have these values”. In this case, our constructor will set the fields name and cell to values theName and theCell.

public class Person
{
    /// fields ////////////////
    String name;
    String cell;

     /////// constructors ////////////////////
    public Person(String theName, String theCell)
    {
       this.name = theName;
       this.cell = theCell;
    }

    //////////// methods ///////////////////////
    public String getName()
    {
       return this.name;
    }

    public void setName(String theName)
    {
       this.name = theName;  //set FIELD to new value
    }

    public String getCell()
    {
       return this.cell;    //return VALUE of field
    }

    public void setCell(String theCell)
    {
       this.cell = theCell;
    }

    public String toString() 
    { 
       return "name: " + this.name +", cell: " + this.cell; 
    }
}

Now we have a full class with fields, methods and a constructor. Run the class code below and play God by creating new People. Notice that you create objects and run code in the Main class, but if you want to change the structure of your people, this must be done in the Person class, in the Person.Java file. Also, note that a class needs to be in a java file with the same name as the class!

The following is the main method for the Person class. It shows two variables (p1 and p2) of type Person being created and each of the variables refers to a new Person object. Each new Person object’s name and phone number are set to the passed values (new Person("Annie", "852 1328-6632")). Then each object’s toString method is called to output information about the object. The toString method is called when you try to print the value of an object using System.out.println(object);