OO Programming with Java


Advanced Concepts

  1. abs is unique so it can be 'singleton' or static:
public class Funs {
  public static int abs(int x) { return (x>=0)?x:-x; }
  public static void main(String[] args) {
    System.out.println(Funs.abs(-3));
  }
}

So, static elements make classes become kind of objects !
A typical example is java.util.Math that proposes abs, cos, exp, ... and don't need to be instanciated multiple times.


3 - 17