1

I am a true beginner in Java. I was recommended the Head First Java book which I started to read. I followed the instructions of the book. I didn't use anything else but notepad and cmd. It all worked fine (type the code in Notepad, save it as ".java", use the javac compiler to compile and the java command to run the program) till I started the second chapter: A trip to Objectville.

Now I have a dilemma: how do I create classes to interact with each other? If I create two classes and try javac on any of them the error is: "missing return statement".

Any help would be much appreciated. Thanks in advance and with gratitude, Alex.

rgettman
  • 176,041
  • 30
  • 275
  • 357
Alex
  • 67
  • 1
  • 2
  • 8
  • Your question has already been answered: http://stackoverflow.com/questions/4800781/how-to-compile-multiple-java-source-files-in-command-line – Andreas Aumayr Oct 25 '13 at 18:38
  • 1
    Please post your code. We can't determine the exact cause of the "missing return statement" error without it. – rgettman Oct 25 '13 at 18:39
  • if you create two classes, you should be atleast getting class not found or cannot find symbol exception. the reason for missing return statement is something else. – Buddha Oct 25 '13 at 18:43

5 Answers5

3

If you want to create two classes that interact with each other without using notepad, you just have to create both the classes in same folder. This is the simplest way of doing it.

You can also compile both source files at once as shown below.

javac a.java b.java

This will compile both java files.

Buddha
  • 4,339
  • 2
  • 27
  • 51
2

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
axiopisty
  • 4,972
  • 8
  • 44
  • 73
2

You need to provide the source code you're having problems with. Use the {} code annotation to render it better.

Now having said that your problem doesn't have to do with 2 classes so much as that you have a class that does not return. You have compile errors not runtime errors. If I have a class like

public int add(int a, int b) {
    int c = a + b;
    return c;
}

You see the "int" after public promises the compiler that I am going to supply a return value. The "return c" part is what fulfills that promise. You are almost certainly missing the "c" (in this example.

Please provide the code and we can help more

Terry
  • 911
  • 10
  • 26
  • {class Dog { int size; String breed; String name; Void bark() { System.out.println("Ham! Ham!"); } } – Alex Oct 25 '13 at 18:48
2

If you get 'missing return statement' it is because your method is declared with non-void return type. Like for instance:

public someType someMethod(){
...
}

And inside it's body there is no return statement. You can do two things about it:

change someType into void or add return someValue; to your method

Luke
  • 1,236
  • 2
  • 11
  • 16
-1

class Dog {

int size;
String breed;   
String name;

Void bark() {

System.out.println("Ham! Ham!");

} }

//this is the fist class

Alex
  • 67
  • 1
  • 2
  • 8
  • 1
    Change "Void" to "void". "Void" with a capital V expects an object of type Void to be returned. Typically "return null" is used as a return statement with "Void". "void" with a lower case v means the method does not return a value. – axiopisty Oct 25 '13 at 18:55
  • 1
    java is case sensitive that means Void is different than void. void is a keyword and Void will be treated like a new type. You probably need to change Void to void. Unless you have somewhere class Void{} declaration. In that case you will still get an error because compiler will expect you to return Void. – Luke Oct 25 '13 at 18:57
  • Rather than posting the code to your original question as an answer, the original post should be edited to contain this code snippet. This clarifies that this is part of the question, not the answer. Also, it is standard practice on StackOverflow to mark the best answer to your question as the "accepted" answer. – axiopisty Oct 25 '13 at 19:00
  • I'm sorry...I haven't used stackoverflow in a while ... I'll be more carefull – Alex Oct 25 '13 at 19:06