OO Programming with Java


Hello World !

Now, the previous code can be re-arranged to introduce a new class/type for persons. Remind that a class' name starts with an uppercase and is saved in a file having the same name:

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

Then recompile and run with:

javac *.java
java Hello
> Hello Kate !
> Hello John !

8 - 17