OO Programming with Java


Primitive datatypes

Data members can be initialized at the declaration as follow, or at the instanciation (2)

public class Person {
  private int birthyear = 2005;
  ...
}

Nb. By default, int, float and double are initialized to 0, and objects to null.
go further

  1. Merging creation and initialisation: the constructor(s)
public class Person {
  private int birthyear;
  public Person(int y) { setBirthyear(y); }
  ...
}
public class Demo {
  public static void main(String args[]) {
    Person p = new Person(2005);
    System.out.println("Birthyear: "+ p.getBirthyear());
  }
}

14 - 17