You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 19 Next »

Below is a short tutorial designed for someone who is familiar with programming (potentially only at the level of MATLAB), and has never learned or long forgotten Java. After explaining the very basics of classes and objects, we skip all of the syntax and control flow and go straight to what you will need to complete the workshop. If your Java is not strong, please take the time to understand this tutorial, otherwise you will most likely be lost during the workshop. It should take about 30 minutes.

What is a class?

In Java, all code is associated with a class. Classes have fields and methods. Here is an example of two simple classes:

Cat.java
public class Cat {
  private int age;
  private String name;
  private Cat friend;

  public Cat(String name, int age) {
    this.name = name;
    this.age = age;
    this.friend = null;
  }

  public String getName() {
    return name;
  }

  public Cat getFriend() {
    return friend;
  }

  public void setFriend(Cat friend) {
    this.friend = friend;
  }

  public String sayHello(String aboutMe) {
    return "Hello World!  My name is " + 
      name + " and I am " + age + 
      " years old." + " I am " + aboutMe + ".";
  }
}
CatsTheMusical.java
public class CatsTheMusical {

  public static void main(String[] args) {
    Cat mist = new Cat("Mr. Mistoffelees", 8);
    System.out.println(mist.sayHello("magical"));
    Cat garf = new Cat("Garfield", 12);
    System.out.println(garf.sayHello("tired/hungry"));
    mist.setFriend(garf);
    System.out.println(mist.getName() + " and " + garf.getName()
        + " are friends? " + areFriends(mist, garf));
    garf.setFriend(mist);
    System.out.println(mist.getName() + " and " + garf.getName()
        + " are friends? " + areFriends(mist, garf));
  }

  public static boolean areFriends(Cat cat1, Cat cat2) {
    if (cat1.getFriend() != null && cat2.getFriend() != null) {
      return cat1.getFriend() == cat2 && cat2.getFriend() == cat1;
    }
    return false;
  }
}

Lets make sure we understand how this works. First, lets talk about the contents of the files Cat.java.

  • Fields: The class Cat has three fields, name, age and friend. Each field has a type, which ensures that we can only store a String in name, an int in age and another Cat in friend. The word private before each field indicates that the field can only be accessed from within the class Cat.
  • Constructors: The class Cat has two constructors {{ public Cat(String name, int age){...} }} and {{public Cat(String name, int age, Cat friend){...} }}. Constructors are used to create instances of the class. Notice that:
  • Methods: The class Cat has two methods, getName(){...} and sayHello(...){..}. The first is an example of a getter method. Note that while there is a getter method, there is no setter method, so once a Cat is created, the name cannot be changed.

Now that we have the basic terminology down, lets examine some of the finer points:

  • Within the class the field (e.g. name) can be accessed simply by calling name or {this.name}}. In Java, by convention most fields are private, and if you want to be able to access them from outside the class, you add getter and setter methods (we will get to these shortly).
  • Because the constructor is public, it can be invoked from other classes. An example of the invoking the constructor can be seen in CatsTheMusical.java.
  • We use the keyword this to set the variables name and age. The arguments of the constructor create variables with the same name as the the fields of the class, shadowing the fields. By using the keyword this, we can access methods and fields associated with the current instance of the class (being built in the constructor). This pattern, while a little confusing, is conventional.

Here, we included a method getName() in class Cat.

  • No labels