OO Programming with Java


Hello World !

In a more usual case, classes represent 'models of objects (also called 'instances' of a class)'. The static keyword breaks this principle by transforming a class into a kind of object. By removing this one, objects are now created by using the new keyword followed by a special operation (having the same name as the class) called a constructor.

  1. Change the code with:
public class Hello {
  public void sayHello(String s) { // INSTANCE's operation
    System.out.println("Hello "+ s +" !");
  }
  public static void main(String args[]) {
    Hello john = new Hello(); // INSTANCIATION
    Hello kate = new Hello();
    john.sayHello("Kate");
    kate.sayHello("John");
  }
}

Then recompile and run with:

java Hello
> Hello Kate !
> Hello John !

7 - 17