Class and instance

Class and instance #

A class can be viewed as a blueprint for a set of similar objects.

Terminology. An object that follows this blueprint is called an instance of the class.

Example. Consider the two following objects (in pseudocode):

{
  name: "Florence",
  zipCode: 50100,
  region: "Tuscany"
}

$\qquad$

{
  name: "Rome",
  zipCode: 00100,
  region: "Lazio"
}

Each of these objects describes a city, and they share the same keys (name, zipCode and region).

A possible class for these two objects may enforce this structure, and it could be named City.

in Java #

Declaration #

In a statically typed language like Java, a class must specify not only the “keys” of its instances, but also their types.

Example (continued). The class City may be declared as follows:

public class City {

  String name;
  int zipCode;
  String region;

  ...
}

Syntax. In Java, a class is often declared in a dedicated file. This file must have the same name as the class (e.g. City.java in this example).

Terminology. In the above example, the three variables (name, zipCode and region) are called attributes (or sometimes member variables).

Constructor #

A class needs a special method called a constructor. The constructor is in charge of creating a (fresh) instance of the class.

Note. A class may have several constructors (with different signatures).

Writing a constructor #

Syntax. The constructor of a Java class must have the same name as the class.

Example (continued).

public class City {

  String name;
  int zipCode;
  String region;

  // constructor for the class `City`
  public City(String n, int z, String r){
    name = n;
    zipCode = z;
    region = r;
  }
  ...
}

Syntax. In this example, the variable names n, z and r are not very explicit. However, using name, zipCode and region would be ambiguous (because these names are already used for the attributes of the class).

In Java, we can use the prefix this. to refer to attribute names, thus eliminating the ambiguity.

Example (continued). The constructor above can equivalently be written:

 public class City {

  String name;
  int zipCode;
  String region;

  public City(String name, int zipCode, String region){
    /* There is no ambiguity in the instruction below:
       - `this.name` refers to the attribute, whereas
       - `name` (on the right-hand side) refers to the argument of the constructor. */
    this.name = name;
    this.zipCode = zipCode;
    this.region = region;
  }
  ...
 }

Calling a constructor #

Syntax. A Java constructor is called with the keyword new.

Example (continued).

 City myCity = new City("Florence", 50100, "Tuscany");
 City yourCity = new City("Rome", 00100, "Lazio");

Warning. In Java (as in Python, C#, etc.), the constructor does not return the instance itself, but a reference (sometimes called “pointer”) to this instance.

Example (continued). In the example above, the variable myCity stores a reference to the object created by the constructor.

To go further. The object that is created in memory by a constructor contains extra information, in particular a reference to its class. This allows type checking, casts, etc.

Default constructor #

If a Java class has no explicit constructor, then a default constructor is generated by the compiler. This constructor has no argument.

Example. The following class has no constructor:

public class GhostTown {

  String name;
}

An instance can nonetheless be created with the default constructor:

GhostTown anonymousCity = new GhostTown();

Accessing an object #

Syntax. The attributes of an object can be accessed like regular variables by using ., followed by the name of the attribute.

Example (continued).

  City thatCity = new City("Siena", 53100, "Lazio");      // create an object
  System.out.println("Wrong region: " + thatCity.region); // access the value of the attribute `region`

  thatCity.region = "Tuscany";                            // modify the value of the attribute `region`
  System.out.println("Better now: " + thatCity.region);

outputs

Wrong region: Lazio
Better now: Tuscany

Reference type vs primitive type #

Reminder. A Java constructor returns a reference to the object that it creates.

Example (continued). The variables myCity, yourCity and thatCity hold a reference, not the object itself.

A variable with an array type (like int[] myArray) also holds a reference (to the array).

Terminology. The types of these variables (e.g. City or int[]) are called reference types.

Types that are not reference types (like int or char) are called primitive types.

Syntax. In Java, a type that start with a capital letter (like City or Set<Integer>) is usually a reference type.

Warning. In Java, the value of a reference cannot be output (printed, displayed, etc.), as opposed to C/C++ for instance.

However, this value can be overwritten or compared to another.

Recall the class City defined above.

What does the following print?

 City aCity = new City("Matera", 75100, "Basilicata");
 City theSameCity = new City("Matera", 75100, "Basilicata");
 System.out.println(aCity == theSameCity);
 System.out.println(aCity.zipCode == theSameCity.zipCode);

Recall the class City defined above.

What does the following print?

 City anotherCity = new City("Bologna", 40100, "Emilia-Romagna");
 City yetAnotherCity = new City("Mantua", 46100, "Emilia-Romagna");

 yetAnotherCity.name = anotherCity.name;
 System.out.println(yetAnotherCity.name);

 anotherCity = yetAnotherCity;
 System.out.println(anotherCity.zipCode);
 System.out.println(anotherCity == yetAnotherCity);