Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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

...

are classes and objects?

In Java, all code is associated with a class. Classes have fields and methods. Instances of classes are created with Constructors and called Objects. Once an Object has been constructed, its methods can be called and its fields can be accessed. Here is an a concrete example of with two simple classes:

Section
borderfalse
Column

Column
width300px
Code Block
titleCat.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 + ".";
  }
}
Column
width10px

Column
width500px
Code Block
titleCatsTheMusical.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;
  }
}
Column

...

  • 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 {{ one constructor 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:. In the constructor, the fields name and age are initialized with the values given in the arguments, while friend is simply set to null.
  • 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.

...