If you get a "missing return statement" you're getting a compile time error instead of a runtime error.
The compile time error you're getting indicates that one of your methods expects a return statement but the method does not have one. Either return what is expected, or change the method signature to void so it does not expect a return value.
Here is an example of how to write multiple objects that interact with each other:
Q19597109.java
/**
* http://stackoverflow.com/questions/19597109/problems-compiling-classes-in-chapter-two-classes-and-objects-of-head-first-jav
*/
public class Q19597109 {
public static void main(String... args) {
Person person = new Person();
person.setName("Bob");
Greeter greeter = new Greeter(person);
greeter.sayHello();
}
}
Person.java
/**
* http://stackoverflow.com/questions/19597109/problems-compiling-classes-in-chapter-two-classes-and-objects-of-head-first-jav
*/
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
Greeter.java
/**
* http://stackoverflow.com/questions/19597109/problems-compiling-classes-in-chapter-two-classes-and-objects-of-head-first-jav
*/
public class Greeter {
private final Person person;
public Greeter(Person person) {
this.person = person;
}
public void sayHello() {
System.out.println("Hello " + person);
}
}
If each of these files is in the same directory then you can do this:
javac *.java
java Q19597109
The output is:
Hello Bob