Java programs are made out of classes. 

Classes have members. 

Members can be: static and non-static. 

Members can be: procedures (methods, functions) and variables. 

Static members exist and are available as soon as we compile the class. 

Non-static members are not there until we create an object.

Non-static members are located in the objects created from the class. 

What are constructors? Constructors are not members of any kind. 

Constructors are in fact initialization procedures for non-static variables. 

Let's see the difference between static and non-static methods. 

Let's remember the exam exercise

  int i = 0; // Connor 
  while (i < 10) ; {
    i = i + 1; 
  }

Is this loop infinite? If not how many iterations until it stops, what is i then? 

Answer: yes. Now let's take a look at: 

  int i = 0; // Ben 
  while (i++ < 10) ; {
    i = i + 1; 
  }
  System.out.println( i ); 

Is this loop infinite? If not how many iterations until it stops, what is i then? 

Answer: no. At the end i will be: __ 12 ___

Now let's take a look at: 

  int i = 0; // Michael 
  while (i > 10) ; {
    i = i + 1; 
  }
  System.out.println( i ); 

Answer: no, the loop does not even start. i is __ 1 __ at the end. 

Back to the original code:

  int i = 0; // Connor
  while (i < 10) ; {
    i = i + 1;
  }


Who writes this kind of code? Beginner student could produce this.

Back to static vs. non-static methods:

class Math {
  // fun : double --> double 
  static double fun(double a) {
     return 2 * a; 
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Math.fun(8)
16.0
> Math.fun(2.25)
4.5



class Math {
  // fun : double --> double 
  static double fun(double a) {
     return 2 * a; 
  }
  double fun(double a) {
     return 2 * a; 
  } 
}

class Math {
  // fun : double --> double 
  static double fun(double a) {
     return 2 * a; 
  }
  double fun(double a, double b) {
     return a + b; 
  } 
}

class Math {
  // fun : double --> double 
  static double fun(double a) {
     return 2 * a; 
  }  
  static double fun(int a) {
     return a - 1; 
  }
  double fun(double a, double b) {
     return 2 * a; 
  } 
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Math.fun(4)
3.0
> Math.fun(4.0)
8.0
>

class Math {
  // fun : double --> double 
  static double fun(double a) {
     return 2 * a; 
  }  
  static double fun(int a) {
     return a - 1; 
  }
  double fun(double a, double b) {
     return a + b; 
  } 
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Math.fun(2.3)
4.6
> Math.fun(1)
0.0
> Math.fun(3, 4)
Static Error: No method in static Math with name 'fun' matches this invocation
    Arguments: (int, int)
    Candidate signatures: 
        double fun(int)
        double fun(double)
>

class Math {
  // fun : double --> double 
  static double fun(double a) {
     return 2 * a; 
  }  
  static double fun(int a) {
     return a - 1; 
  }
  double fun(double a, double b) {
     return a + b; 
  } 
}


Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> new Math()
Math@110f98f
> ( new Math() ).fun(3, 4)
7.0
> Math math
> math = new Math()
Math@18ef8a3
> math.fun(4, 5)
9.0
> math.fun(-1, 7)
6.0



class Math {
  // fun : double --> double 
  static double fun(double a) {
     return 2 * a; 
  }  
  static double fun(int a) {
     return a - 1; 
  }
  double fun(double a, double b) {
     return a + b; 
  } 
}


Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Math.fun(3)
2.0
> Math.fun(3.0)
6.0
> Math a = new Math()
> a.fun(8, 3)
11.0
> a.fun(4)
3.0


Task: in a class Math provide the definition for sqrt(...) a method that
calculates the square root of its argument. 

class Math {
  static double sqrt(double number) {
    return java.lang.Math.sqrt( number );  
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Math.sqrt(3)
1.7320508075688772
> java.lang.Math.sqrt(3)
1.7320508075688772


class Math {
  static double sqrt(double number) {
    double precision = 0.000001, guess;
    for ( guess = number - precision; 
         guess * guess >= number ; 
         guess -= precision) ;
    return guess; 
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Math.sqrt(2)
1.4142130000481907
> java.lang.Math.sqrt(2)
1.4142135623730951
> Math.sqrt(3)
1.7320499998822656
> java.lang.Math.sqrt(3)
1.7320508075688772
> java.lang.Math.sqrt(1000000)
1000.0
> java.lang.Math.sqrt(1000000000)
31622.776601683792
> java.lang.Math.sqrt(10000000000000)
Literal is out of range: 10000000000000
> java.lang.Math.sqrt(10000000000)
Literal is out of range: 10000000000
> java.lang.Math.sqrt(100000000)
10000.0
> java.lang.Math.sqrt(10000000000000.0)
3162277.6601683795
> java.lang.Math.sqrt(100000000000000000.0)
3.1622776601683795E8
> java.lang.Math.sqrt(2)
1.4142135623730951